On Thu, 13 Mar 2008, Dankinit wrote:
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:
has_many :business, :through => :list_items
Plural: has_many :businesses, etc.
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).
- Is belongs_to list_items ok for my model above?
No – see above.
- 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:
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.
Thanks for any help!
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!