Teej
Teej

Reputation: 12893

Association Structure

I currently have associations like this:

Category belongs_to User User has_many Categories

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.

Upvotes: 1

Views: 45

Answers (1)

Ashish
Ashish

Reputation: 5791

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'

Upvotes: 1

Related Questions