我在这里有一个令人困惑的问题。我有两个模型,具有 has_one 关系。我有一个使用 fields_for 创建子实例的表单。但是,当我尝试从子模型访问父模型时,它只会得到零。
我试图为以下问题提供一个简洁明了的示例:
class Parent
has_one :child
accepts_nested_attributes_for :child
attr_accessible :child_attributes
end
class Child
belongs_to :parent
validate :parent_is_called_mum
def parent_is_called_mum
parent.name.equals?("mum")
end
end
问题是parent.name.equals?("mum")
返回错误:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name
为什么关系返回为零?