# Belongs\_to has\_many newbie question

**URL:** <https://www.ruby-forum.com/t/belongs-to-has-many-newbie-question/131882>\
**Category:** Rails\
**Created:** [March 14, 2008, 3:16am UTC](https://www.ruby-forum.com/t/belongs-to-has-many-newbie-question/131882 "2008-03-14T03:16:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dankinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@Dankinit](https://www.ruby-forum.com/u/Dankinit)\
**Post date:** [March 14, 2008, 3:16am UTC](https://www.ruby-forum.com/t/belongs-to-has-many-newbie-question/131882/1 "2008-03-14T03:16:50Z")

</div>

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.’.

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![Dankinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@Dankinit](https://www.ruby-forum.com/u/Dankinit)\
**Post date:** [March 14, 2008, 4:19am UTC](https://www.ruby-forum.com/t/belongs-to-has-many-newbie-question/131882/2 "2008-03-14T04:19:00Z")

</div>

Hi –

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:
> 
> 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.

> 1. 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.

David

> 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](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![Dankinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@Dankinit](https://www.ruby-forum.com/u/Dankinit)\
**Post date:** [March 15, 2008, 1:04am UTC](https://www.ruby-forum.com/t/belongs-to-has-many-newbie-question/131882/3 "2008-03-15T01:04:57Z")

</div>

Thank you so much David, it all worked perfectly!

Much appreciated!

Dan
