# Display variable from a relationship

**URL:** <https://www.ruby-forum.com/t/display-variable-from-a-relationship/116824>\
**Category:** Rails\
**Created:** [October 10, 2007, 2:36am UTC](https://www.ruby-forum.com/t/display-variable-from-a-relationship/116824 "2007-10-10T02:36:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flamingsole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@flamingsole](https://www.ruby-forum.com/u/flamingsole)\
**Post date:** [October 10, 2007, 2:36am UTC](https://www.ruby-forum.com/t/display-variable-from-a-relationship/116824/1 "2007-10-10T02:36:35Z")

</div>

Hello,

I have the following scenario:

Models:

provider (has\_and\_belongs\_to\_many)  
level (has\_and\_belongs\_to\_many)  
levels\_providers

At the moment, I’m attempting a page that will list all of the  
providers. I have this working perfectly, but I want each provider that  
is listed to also list the levels that are associated with it. I haven’t  
been able to get my head around this. I can provide more information,  
but I’m not sure where to go at this point. Thanks for any help!

Jonathan

---

<div class="post-metadata">

**Author:** ![flamingsole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@flamingsole](https://www.ruby-forum.com/u/flamingsole)\
**Post date:** [October 10, 2007, 4:52am UTC](https://www.ruby-forum.com/t/display-variable-from-a-relationship/116824/2 "2007-10-10T04:52:04Z")

</div>

Assuming the models are set up correctly each provider object can  
access it’s levels in something similar to:

for level in provider.levels do  
#stuff  
end

provider.levels should give you every level associated with that  
provider.

Similarly, level.providers will give you all the providers associated  
with any given leve.

On Oct 9, 7:36 pm, Jonathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![flamingsole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@flamingsole](https://www.ruby-forum.com/u/flamingsole)\
**Post date:** [October 10, 2007, 5:40am UTC](https://www.ruby-forum.com/t/display-variable-from-a-relationship/116824/3 "2007-10-10T05:40:21Z")

</div>

Perfect. Thanks for your help.

removed\_email\_address@domain.invalid wrote:

> Assuming the models are set up correctly each provider object can  
> access it’s levels in something similar to:
> 
> for level in provider.levels do  
> #stuff  
> end
> 
> provider.levels should give you every level associated with that  
> provider.
> 
> Similarly, level.providers will give you all the providers associated  
> with any given leve.
> 
> On Oct 9, 7:36 pm, Jonathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
