I am trying to populate a nested field, product_name, it an Item.
In my Item model:
class Item < ActiveRecord::Base
attr_writer :product_name
belongs_to :order
belongs_to :product
def product_name
#Product.find_by_id(self.product_id) #=> returns the product object
#self.product #=> returns the product object
#Product.find_by_id(self.product_id).name #=> complains that 'name' is being called on nil
#self.product.name #=> complains that 'name' is being called on nil
#"foooobar" #=> returns "foooobar"
end
end
Each of those commented lines behaves as described.
What I don’t get is, how can something return an object successfully, but then complain that the object is nil when you access an attribute of it?
Here is the whole error:
NoMethodError in Orders#edit
Showing app/views/orders/_form.haml where line #18 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #18):
17: = item.hidden_field :product_id
18: = item.text_field :product_name, :class => 'auto_complete_input'
19: = item.text_field :quantity, :size => 2
Thanks
It sounds like you’re asking a really general question. If you’re asking a totally different question than I’m hearing, ignore this. 🙂
Yes, Ruby is successfully returning an object, and the object is
nil. In Ruby, a method can return any value, andnilis just another value that a method can return. Those statements are probably doing a query that isn’t returning any results, so the method returnsnilinstead of the object you are expecting.The longer answer is that
Product.find_by_id(x)calls a generated find_by_ attribute method for theidfield, which is the same as callingProduct.find(:first, :conditions => ["id = ?", x]). If you look at the documentation for .find, you will notice that it says