与accepts_nested_attributes_for麻烦的验证外键 [英] Trouble with accepts_nested_attributes_for on validating foreign key
问题描述
我使用Ruby on Rails的V3.2.2。我想解决使用 accepts_nested_attributes_for
和 validates_associated
RoR的方法时,涉及到外键的验证问题。也就是说,我有以下模型类:
类文章<的ActiveRecord :: Base的
的has_many:category_associations,:foreign_key => CATEGORY_ID
accepts_nested_attributes_for:category_associations,:reject_if =>拉姆达{|属性|属性[:CATEGORY_ID] .blank? }
validates_associated:category_associations
结束
类CategoryAssociation<的ActiveRecord :: Base的
belongs_to的:文章:foreign_key => 的article_id
belongs_to的:类别:foreign_key => CATEGORY_ID
验证:article_id的,:presence =>真正
验证:CATEGORY_ID,:presence =>真正
结束
......,我有以下控制措施:
类ArticlesController<的ApplicationController
高清新
@article = Article.new
5.times {@ article.category_associations.build}
#...
结束
DEF创建
@article = Article.new(PARAMS [:文章])
如果@ article.save
#...
其他
#...
结束
结束
结束
通过上面的code(启发由嵌套模式组成部分1 Rails的角色)的我的意图是建立一个文章的时候存储类别关联的(注意的:类对象已经present在数据库中;在我的情况,我想只是存储创造类别关联)。然而,当我从相关视图文件提交相关表格,我得到以下错误(我记录错误消息):
{:category_associations.article_id=>不能为空]:category_associations =>无效]}
为什么会发生,因为 validates_associated
<一个href="http://stackoverflow.com/questions/5176510/validates-associated-not-checking-existence-of-associations#comment5826558_5176607">seems运行的方法 article.category_association.valid?
但只有当 article.category_association.article_id
是不的零
?我该如何解决与的article_id
外键的presence验证问题?
不过,如果我注释掉验证:article_id的,:presence =&GT;真正的
在 CategoryAssociation
模型类,它的工作原理是预期,但<一个href="http://stackoverflow.com/questions/13345250/is-it-really-needed-to-validate-foreign-keys/13346791#13346791">it似乎是的没有的一个正确的方法做的没有的验证外键的。
如果我注释掉 validates_associated:在
,我仍然得到错误:文章
模型类category_associations
{:category_associations.article_id=&GT;不能为空]}
使用 inverse_of
链接协会和验证presence关联的对象,而不是实际的外键presence。
从的文档:
类会员&LT;的ActiveRecord :: Base的
的has_many:帖子,inverse_of:会员
accepts_nested_attributes_for:帖子
结束
类岗位&LT;的ActiveRecord :: Base的
belongs_to的:会员,inverse_of:帖子
validates_ presence_of:成员
结束
I am using Ruby on Rails v3.2.2. I would like to solve the issue related to the validation of a foreign key when using accepts_nested_attributes_for
and validates_associated
RoR methods. That is, I have following model classes:
class Article < ActiveRecord::Base
has_many :category_associations, :foreign_key => 'category_id'
accepts_nested_attributes_for :category_associations, :reject_if => lambda { |attributes| attributes[:category_id].blank? }
validates_associated :category_associations
end
class CategoryAssociation < ActiveRecord::Base
belongs_to :article, :foreign_key => 'article_id'
belongs_to :category, :foreign_key => 'category_id'
validates :article_id, :presence => true
validates :category_id, :presence => true
end
... and I have following controller actions:
class ArticlesController < ApplicationController
def new
@article = Article.new
5.times { @article.category_associations.build }
# ...
end
def create
@article = Article.new(params[:article])
if @article.save
# ...
else
# ...
end
end
end
With the above code ("inspired" by the Nested Model Form Part 1 Rails Cast) my intent is to store category associations when creating an article (note: category objects are already present in the database; in my case, I would like just storing-creating category associations). However, when I submit the related form from the related view file, I get the following error (I am logging error messages):
{:"category_associations.article_id"=>["can't be blank"], :category_associations=>["is invalid"]}
Why it happens since validates_associated
seems to run the method article.category_association.valid?
but only if the article.category_association.article_id
is not nil
? How can I solve the problem with the presence validation of the article_id
foreign key?
However, if I comment out the validates :article_id, :presence => true
in the CategoryAssociation
model class, it works as expected but it seems to be not a right approach to do not validate foreign keys.
If I comment out the validates_associated :category_associations
in the Article
model class, I still get the error:
{:"category_associations.article_id"=>["can't be blank"]}
Use inverse_of
to link the associations and validate the presence of the associated object, NOT the presence of the actual foreign key.
Example from the docs:
class Member < ActiveRecord::Base
has_many :posts, inverse_of: :member
accepts_nested_attributes_for :posts
end
class Post < ActiveRecord::Base
belongs_to :member, inverse_of: :posts
validates_presence_of :member
end
这篇关于与accepts_nested_attributes_for麻烦的验证外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!