播种验证失败嵌套表(validates_ presence_of) [英] Seeding fails validation for nested tables (validates_presence_of)

查看:238
本文介绍了播种验证失败嵌套表(validates_ presence_of)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是组织模式有一个1:用户模式一对多的关联。
我有以下验证我的用户模型文件:

  belongs_to的:组织
validates_ presence_of:organization_id,:除非=> 用户类型== 1'

如果用户类型为1时,这意味着用户将没有关联到它的组织。对于不同的用户类型的organization_id的presence应该是强制性的。

该组织模型包括:

 的has_many:用户
accepts_nested_attributes_for:用户:reject_if => :all_blank,:allow_destroy =>真正

我的种子文件使用嵌套,其中包括:

  Organization.create(名称:虚构业务,
                     地址:法克尔:: Address.street_address,
                     城市:法克尔:: Address.city,
  users_attributes:[邮件:helpst@example.com
                     用户名:helpyzghtst
                     用户类型:2,
                     密码指定:foobar
                     password_confirmation指定:foobar])

在此播种下面生成错误。从模型中取出验证解决它,但我并不想这样做。我怎样才能解决这个问题?


  

验证失败:用户的组织不能为空



解决方案

发现这一点:的验证Rails中(最后一章)嵌套协会

 类用户
  belongs_to的:组织,inverse_of:用户
  validates_ presence_of:organization_id,:除非=> 用户类型== 1'
结束一流的组织
  的has_many:用户
  accepts_nested_attributes_for:用户:reject_if => :all_blank,:allow_destroy =>真正
结束

该文档是不是说得很清楚,但我认为这是值得一试。看到这个<一个href=\"http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to#974-Undocumented-inverse-of-option\"相对=nofollow>注释,指出该协会将望风使用的对象在内存中,而不是从数据库中获取它们,这将是你所需要的。

修改

删除inverse_of本组织类。

An Organization model has a 1:many association with a User model. I have the following validation in my User model file:

belongs_to :organization
validates_presence_of :organization_id, :unless => 'usertype==1'

If usertype is 1, it means the user will have no organization associated to it. For a different usertype the presence of an organization_id should be mandatory.

The organization model includes:

has_many :users
accepts_nested_attributes_for :users, :reject_if => :all_blank, :allow_destroy => true

My seeds file uses nesting and includes:

Organization.create!(name: "Fictious business",
                     address: Faker::Address.street_address,
                     city: Faker::Address.city,
  users_attributes: [email: "helpst@example.com",
                     username: "helpyzghtst", 
                     usertype: 2,
                     password: "foobar", 
                     password_confirmation: "foobar"])

On seeding this generates the error below. Removing the validation from the model solves it, but I don't want to do that. How can I solve this?

Validation failed: Users organization can't be blank

解决方案

Found this : Validating nested association in Rails (last chapter)

class User
  belongs_to :organization, inverse_of: :users
  validates_presence_of :organization_id, :unless => 'usertype==1'
end

class Organization
  has_many :users
  accepts_nested_attributes_for :users, :reject_if => :all_blank, :allow_destroy => true
end

The documentation is not quite clear about it but I think it's worth a try. see this comment, stating that the association lookout will use the objects in memory and not fetch them from the database, which would be what you need.

EDIT

removed inverse_of on the Organization class.

这篇关于播种验证失败嵌套表(validates_ presence_of)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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