Ruby on rails Rails接受子对象的嵌套属性;验证时未设置父项
验证时,我正在尝试访问子模型中的父模型。我在has_one上发现了一些关于反向属性的东西,但是我的Rails 2.3.5没有识别出它,所以它一定没有进入发行版。但我不确定这是否正是我所需要的 我想根据父属性有条件地验证子项。我的父模型已创建。如果在更新父对象上的_属性时未创建子对象,则该子对象无权访问父对象。我想知道如何才能访问此家长。这应该很容易,比如parent.build\u child设置子模型的parent\u id,为什么在为接受嵌套的属性构建子模型时不这样做呢 例如:Ruby on rails Rails接受子对象的嵌套属性;验证时未设置父项,ruby-on-rails,activerecord,relationships,Ruby On Rails,Activerecord,Relationships,验证时,我正在尝试访问子模型中的父模型。我在has_one上发现了一些关于反向属性的东西,但是我的Rails 2.3.5没有识别出它,所以它一定没有进入发行版。但我不确定这是否正是我所需要的 我想根据父属性有条件地验证子项。我的父模型已创建。如果在更新父对象上的_属性时未创建子对象,则该子对象无权访问父对象。我想知道如何才能访问此家长。这应该很容易,比如parent.build\u child设置子模型的parent\u id,为什么在为接受嵌套的属性构建子模型时不这样做呢 例如: class
class Parent < AR
has_one :child
accepts_nested_attributes_for :child
end
class Child < AR
belongs_to :parent
validates_presence_of :name, :if => :some_method
def some_method
return self.parent.some_condition # => undefined method `some_condition' for nil:NilClass
end
end
查看这些网站,也许它们会帮助你 看起来,rails将在子验证成功后分配父\u id。 (因为父项在保存后有一个id) 也许值得一试:
child.parent.some_condition
而不是self.parent.some_条件。。。谁知道…您不能这样做,因为在内存中,子对象不知道分配给它的父对象。它只有在保存后才知道。比如说
child = parent.build_child
parent.child # => child
child.parent # => nil
# BUT
child.parent = parent
child.parent # => parent
parent.child # => child
因此,您可以通过手动执行反向关联来强制这种行为。比如说
def child_with_inverse_assignment=(child)
child.parent = self
self.child_without_inverse_assignment = child
end
def build_child_with_inverse_assignment(*args)
build_child_without_inverse_assignment(*args)
child.parent = self
child
end
def create_child_with_inverse_assignment(*args)
create_child_without_inverse_assignment(*args)
child.parent = self
child
end
alias_method_chain :"child=", :inverse_assignment
alias_method_chain :build_child, :inverse_assignment
alias_method_chain :create_child, :inverse_assignment
如果你真的觉得有必要
另外,它现在不这么做是因为它不太容易。需要明确告诉它如何在每个特定情况下访问父/子。使用身份映射的综合方法可以解决这个问题,但对于较新的版本,有一个的解决方法。在新闻组中进行了一些类似的讨论。我也遇到了类似的问题:
这就是我最终解决问题的方式;通过在回调上设置父级
class Parent < AR
has_one :child, :before_add => :set_nest
accepts_nested_attributes_for :child
private
def set_nest(child)
child.parent ||= self
end
end
class父类:set\u nest
接受\u嵌套的\u属性\u:child
私有的
def集合_嵌套(子项)
child.parent | |=self
结束
结束
我在Rails 3.2中遇到了基本相同的问题。正如问题中所建议的,将inverse\u of
选项添加到父级的关联中,为我解决了这个问题
应用于您的示例:
class Parent < AR
has_one :child, inverse_of: :parent
accepts_nested_attributes_for :child
end
class Child < AR
belongs_to :parent, inverse_of: :child
validates_presence_of :name, :if => :some_method
def some_method
return self.parent.some_condition # => undefined method `some_condition' for nil:NilClass
end
end
class父类:some方法的存在性
定义某些方法
返回self.parent.some_condition#=>nil:NilClass的未定义方法'some_condition'
结束
结束
我遇到了与op相同的错误,但当我尝试此方法时,我得到了“未知密钥:在添加之前”?
class Parent < AR
has_one :child, :before_add => :set_nest
accepts_nested_attributes_for :child
private
def set_nest(child)
child.parent ||= self
end
end
class Parent < AR
has_one :child, inverse_of: :parent
accepts_nested_attributes_for :child
end
class Child < AR
belongs_to :parent, inverse_of: :child
validates_presence_of :name, :if => :some_method
def some_method
return self.parent.some_condition # => undefined method `some_condition' for nil:NilClass
end
end