Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

missing attribute in activerecord

I have models Foo and Bar. Bar has column foo_id. When I call Bar.foo_id I get the error missing attribute: foo_id

Keep in mind that this is not an undefined method error and the column definitely is in the db. What are some common causes of this?

Thanks

like image 865
user94154 Avatar asked Aug 25 '09 03:08

user94154


2 Answers

Probably it has something to do with your find method? For instance you did a :select in a find:

Foo.find(:all, :select => "firstvar, secondvar")

In that case, you can only access firstvar and secondvar even though you have foo_id defined

Hope it helps! =)

like image 120
Staelen Avatar answered Nov 09 '22 23:11

Staelen


Are you calling

Bar.foo_id

or

bar = Bar.new
bar.foo_id

Unless you have a class variable for Bar, you need to look at foo_id on an instance of Bar. I hope that helps. Cheers.

like image 36
theIV Avatar answered Nov 10 '22 01:11

theIV