开发者

Association Structure

I currently have associations like this:

Category belongs_to User User has_many Categories

开发者_JS百科

A User can create a category

However, The problem I am having is how do I associate Users to their liked categories. A User can like a Category even if it is created by another User.

I am looking into creating another Model with

user_id and category_id

How do I go about creating the Model and how would the relationship be? A Category should still belongs_to to a User but a User can like a Category.


According to your requirements there should be two relationships.

1. User has_many :categories  and 
   Category belongs_to :user
2. User habtm :favorite_cats, :class_name => 'Category'  and 
   Category habtm :liked_by_users, :class_name => 'User'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜