Reputation: 16574
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
Upvotes: 15
Views: 16881
Reputation: 5196
This happens to me now and then. My cause is almost always a long forgotten finder method on a class that specifies fields in .select() and the missing attribute (often added long after I had the finder defined) is now invoked somewhere in the app but not specified in that .select().
The error is common enough to have earned an alert in the Rails Guides: "Be careful because ... "
Upvotes: 2
Reputation: 7841
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! =)
Upvotes: 45
Reputation: 25794
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.
Upvotes: 2