0

I am working on an application wherein I am stuck deciding on a relationship. The situation is this:

The implemented relationships are:

Category has_many Items
Outlets HABTM Categories

The problem is that Outlets needs to have many-to-many relationship with the Item model. Now i can implement it simply like:

Outlet HABTM Items

which will give me a table with outlet_id and item_id but in that case I am not sure how to find out the list of items for an outlet for one of the categories assigned to it.

I also saw the has_many :through example but it also suggests having two foreign keys in the table; I believe that there should be three in this case, outlet_id, category_id, item_id, where in the combination of all 3 will be unique.

I can implement this easily using raw SQL queries but i haven't use any raw queries yet, and want to avoid as much as possible. How can I properly do this using model relationships?

Muhammad Ali
  • 2,173
  • 15
  • 20
Pawan Rawat
  • 495
  • 1
  • 7
  • 25

1 Answers1

0

I would do it simply like :

Outlet has_many categories & Category has_many items

This way we can get all the items for a specific category in an outlet and also can get all items for an outlet through categories.

Muhammad Ali
  • 2,173
  • 15
  • 20