I've searched alot tonight for this answer, it must be out there, so
thanks for any help.
I have a simple setup like so:
LIST
has_many :list_items
has_many :business, :through => :list_items
LIST_ITEM
belongs_to :list
belongs_to :business
BUSINESS
belongs_to :list_items ...?
So I have busineses on my website. Now I've made some lists, that
have list items (which are businesses).
1. Is belongs_to list_items ok for my model above?
2. If i'm in the business controller viewing a business, how can i
see what lists that business is listed in? So in my view in business,
i want to show 'this business is listed in list X, list Y, etc.'.
I've searched alot tonight for this answer, it must be out there, so
thanks for any help.
I have a simple setup like so:
LIST
has_many :list_items
has_many :business, :through => :list_items
Plural: has_many :businesses, etc.
LIST_ITEM
belongs_to :list
belongs_to :business
BUSINESS
belongs_to :list_items ...?
That should be has_many :list_items. I think you'll also want
has_many :lists, :through => :list_items
So I have busineses on my website. Now I've made some lists, that
have list items (which are businesses).
1. Is belongs_to list_items ok for my model above?
No -- see above.
2. If i'm in the business controller viewing a business, how can i
see what lists that business is listed in? So in my view in business,
i want to show 'this business is listed in list X, list Y, etc.'.
If you have a business object in the variable @business, and if you do
has_many :lists as above, then you can do:
@business.lists
and that will give you a collection (array) of all the lists.
I kind of think that maybe you want businesses to have lists and to
have list_items through lists, rather than the other way around, but
I'm not sure. Anyway, you can follow the patterns here. The goal is to
make it easy to get the information from the objects you've got.