验证当Rails的accepts_nested_attributes_for孩子没有父母集 [英] Rails accepts_nested_attributes_for child doesn't have parent set when validating

查看:91
本文介绍了验证当Rails的accepts_nested_attributes_for孩子没有父母集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证的时候访问我父模型在我子模型。我发现了一些有关的HAS_ONE逆属性,但我的Rails 2.3.5不承认它,所以它必须有没有把它变成释放。我不知道这是否正是我需要的,但。

我想验证有条件基于父属性的孩子。我父模型已经建立。如果孩子没有被创建时我的update_attributes对父,那么它不具有访问父。我不知道我怎么可以访问此父。它的的容易,像parent.build_child设置子模型的PARENT_ID,为什么不建立孩子accepts_nested_attributes_for的时候做什么呢?

例如:

 类家长和LT; AR
  HAS_ONE:孩子
  accepts_nested_attributes_for:孩子
结束
一流的子< AR
  belongs_to的:父母
  validates_ presence_of:名:如果=> :some_method

  高清some_method
    返回self.parent.some_condition#=>未定义的方法`some_condition'的零:NilClass
  结束
结束
 

我的方式是标准:

 <%的form_for @parent办| F | %>
  <%f.fields_for:孩子办| C | %>
    &其中;%= c.name%GT;
  <%结束%GT;
<%结束%GT;
 

使用的更新方法

 高清更新
  @parent = Parent.find(PARAMS [:ID])
  @ parent.update_attributes(PARAMS [:父])#=>这就是我的孩子验证发生
结束
 

解决方案

您不能这样做,因为内存的孩子不知道它分配给父。它只能保存后,知道了。例如,

 子= parent.build_child
parent.child#=>儿童
child.parent#=>零

# 但
child.parent =父
child.parent#=>父
parent.child#=>儿童
 

所以,你可以通过手动执行反向关联那种强制此行为。例如

 高清child_with_inverse_assignment =(子)
  child.parent =自
  self.child_without_inverse_assignment =小孩
结束

高清build_child_with_inverse_assignment(*参数)
  build_child_without_inverse_assignment(*参数)
  child.parent =自
  儿童
结束

高清create_child_with_inverse_assignment(*参数)
  create_child_without_inverse_assignment(*参数)
  child.parent =自
  儿童
结束

alias_method_chain:孩子=:inverse_assignment
alias_method_chain:build_child,:inverse_assignment
alias_method_chain:create_child,:inverse_assignment
 

如果你真的觉得有必要的。

P.S。它没有做,现在的原因是因为它不是太容易了。它需要被明确告知如何访问父/子在每个具体病例。身份映射一个COM prehensive办法已经解决了这个问题,但对于较新的版本有:inverse_of 解决方法。像<一个部分讨论href="http://groups.google.com/group/rubyonrails-core/browse_thread/thread/eaba411bb69c58d8?fwc=1">this 之一发生在新闻组。

I'm trying to access my parent model in my child model when validating. I found something about an inverse property on the has_one, but my Rails 2.3.5 doesn't recognize it, so it must have never made it into the release. I'm not sure if it's exactly what I need though.

I want to validate the child conditionally based on parent attributes. My Parent model has already been created. If the child hasn't been created when I update_attributes on the parent, then it doesn't have access to the parent. I'm wondering how I can access this parent. It should be easy, something like parent.build_child sets the parent_id of the child model, why is it not doing it when building the child for accepts_nested_attributes_for?

For Example:

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

My form is standard:

<% form_for @parent do |f| %>
  <% f.fields_for :child do |c| %>
    <%= c.name %>
  <% end %>
<% end %>

With an update method

def update
  @parent = Parent.find(params[:id])
  @parent.update_attributes(params[:parent])   # => this is where my child validations take place
end

解决方案

You cannot do this because in-memory child doesn't know the parent its assigned to. It only knows after save. For example.

child = parent.build_child
parent.child # => child
child.parent # => nil

# BUT
child.parent = parent
child.parent # => parent
parent.child # => child

So you can kind of force this behavior by doing reverse association manually. For example

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

If you really find it necessary.

P.S. The reason it's not doing it now is because it's not too easy. It needs to be explicitly told how to access parent/child in each particular case. A comprehensive approach with identity map would've solved it, but for newer version there's :inverse_of workaround. Some discussions like this one took place on newsgroups.

这篇关于验证当Rails的accepts_nested_attributes_for孩子没有父母集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆