I’m developing a voting feature for the website in design for YouTube “Likes” and “Dislikes” and Digg using Ruby on Rails 3. I've getting trouble approaching using the right plan.

I've three models, Customers, Subjects, and Votes. Each User can make one election “Like” or “Dislike” for just one Subject. Such as these sites, Customers can Election on the Subject, however they may also create new Subjects. I’d prefer to have the ability to not just view all a User’s Votes, but additionally both Subjects they've produced and also the Subjects they've chosen on. I'm attempting to build this by ourselves and choose just how to create the database additional process.

My first idea ended up being to use :has_many and goes_to solely like so…

class User < ActiveRecord::Base

has_many :votes

has_many :subjects

class Subject < ActiveRecord::Base

has_many :votes

goes_to :customers

class Election < ActiveRecord::Base

goes_to :subjects

goes_to :customers

boolean choice #tracks if the User selects Like or Dislike

However it grew to become apparent this is probably not the easiest method to do that. I started to consider the best way is always to make use of a :has_many :through association like...

class User < ActiveRecord::Base

has_many :votes, :through => :subjects

But I am not sure. Any applying for grants just how to create something similar to this up?

I believe your initial setup is nice but it may be further enhanced upon to higher support what you would like to complete. Possibly such as this:

class User < ActiveRecord::Base
  has_many :votes
  has_many :topics

  #Lists all topics the user has voted on
  has_many :voted_topics, :through => :votes, :source => :topic

  #Lists all votes for the users topics
  has_many :topic_votes, :through => :topics, :source => :votes
end

class Topic < ActiveRecord::Base
  has_many :votes
  belongs_to :user
end

class Vote < ActiveRecord::Base
  belongs_to :topic
  belongs_to :user
end