How do you model "Likes" in rails?

Viewed 5552

I have 3 models: User, Object, Likes

Currently, I have the model: a user has many Objects. How do I go about modeling:

1) A user can like many objects

2) an Object can have many likes (from different users)

So I want to be able to do something like this:

User.likes = list of objects liked by a user

Objects.liked_by = list of Users liked by object

The model below is definitely wrong...

class User < ActiveRecord::Base
  has_many :objects
  has_many :objects, :through => :likes
end

class Likes < ActiveRecord::Base
  belongs_to :user
  belongs_to :object
end

class Objects < ActiveRecord::Base
  belongs_to :users
  has_many :users, :through => :likes    
end
4 Answers
Related