OhMyRuby
(OhMyRuby)
April 7, 2008, 2:18pm
1
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?
Bryan_Ray
(Bryan Ray)
April 7, 2008, 3:01pm
2
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.
OhMyRuby
(OhMyRuby)
April 7, 2008, 4:10pm
3
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.
OhMyRuby
(OhMyRuby)
April 7, 2008, 4:28pm
4
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.
11175
(-- --)
April 7, 2008, 4:59pm
5
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