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?