# Access association data via instance variable

**URL:** <https://www.ruby-forum.com/t/access-association-data-via-instance-variable/134063>\
**Category:** Rails\
**Created:** [April 7, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/access-association-data-via-instance-variable/134063 "2008-04-07T16:19:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 7, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/access-association-data-via-instance-variable/134063/1 "2008-04-07T16:19:04Z")

</div>

i thought this was possible but it is not working for me.

@items = Items.find(:all)

in the view via a loop the goes thru all the rows.

item.relationship.related\_table\_col

this The error occurred while evaluating nil.name

if i pass @one\_item = Items.find(402)

then it works.

how do i set up this loop so that this would work?

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 7, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/access-association-data-via-instance-variable/134063/2 "2008-04-07T17:02:54Z")

</div>

You’ll need to paste your view code and get a little more detail on what  
you’re doing.

I’ll point out a couple things though …

On Mon, Apr 7, 2008 at 9:18 AM, rashantha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i thought this was possible but it is not working for me.
> 
> @items = Items.find(:all)

Model name’s are typically in the singular form … this would look  
like:

@items = Item.find :all

> in the view via a loop the goes thru all the rows.
> 
> item.relationship.related\_table\_col
> 
> this The error occurred while evaluating nil.name

It sounds as if you’re getting :all of the items, but one of them has a  
missing foreign key association in the database.

> [http://nil.name](http://nil.name)
> 
> if i pass @one\_item = Items.find(402)
> 
> then it works.
> 
> how do i set up this loop so that this would work?
> 
> >

–  
Bryan R.

> **[BryanRay.NET](https://www.bryanray.net/)**
>
> I'm not a great developer, I'm a decent developer with great habits.

“Programming today is a race between software engineers striving to  
build  
bigger and better idiot-proof programs, and the Universe trying to  
produce  
bigger and better idiots. So far, the Universe is winning.”

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 7, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/access-association-data-via-instance-variable/134063/3 "2008-04-07T18:11:25Z")

</div>

> Model name’s are typically in the singular form … this would look like:
> 
> @items = Item.find :all

yes, i made a mistake on the post but that is accurate in the  
controller.

> It sounds as if you’re getting :all of the items, but one of them has a  
> missing foreign key association in the database.

can you please explain this. i am not following what you are saying.

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 7, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/access-association-data-via-instance-variable/134063/4 "2008-04-07T18:59:59Z")

</div>

rashantha wrote:

> SOLVED.
> 
> thanks for the clue. bryan
> 
> yes, some of the associations were nil and i had to check for this  
> before outputting.  
> so i did a an if statement and all works well thru the loop. yuk 2  
> days.
> 
> if item.some\_id == 0  
> no relationship  
> else  
> item.relationship.variable  
> end.
> 
> thanks.

It’s probably a bit cleaner to just check whether the association is nil

if item.foo  
item.foo.variable  
end

or even a simple 1 liner

item.foo.variable if item.foo

Or this would even work

item.foo && item.foo.variable

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 7, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/access-association-data-via-instance-variable/134063/5 "2008-04-07T18:29:01Z")

</div>

SOLVED.

thanks for the clue. bryan

yes, some of the associations were nil and i had to check for this  
before outputting.  
so i did a an if statement and all works well thru the loop. yuk 2  
days.

if item.some\_id == 0  
no relationship  
else  
item.relationship.variable  
end.

thanks.
