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 …

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.

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.

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.

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