Access association data via instance variable

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?

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 [email protected] 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

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.
http://www.bryanray.net

“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.”

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.

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs