- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个简单的联想:
class Account < ActiveRecord::Base
has_many :users
accepts_nested_attributes_for :users
validates_presence_of :users
end
和
class User < ActiveRecord::Base
belongs_to :account
end
我只想运行一个简单的测试:
describe 'a new', Account do
it 'should be valid' do
Factory.build(:account).should be_valid
end
end
与工厂:
Factory.define :account do |a|
a.name { Faker::Company.name }
end
Factory.define :user do |u|
u.association :account
u.email { Faker::Internet.email }
end
但我总是遇到这个错误:
'a new Account should be valid' FAILED
Expected #<Account id: nil, name: "Baumbach, Gerlach and Murray" > to be valid, but it was not
Errors: Users has to be present
好吧,我设置了正确的关联,但它不起作用...
感谢您的帮助。
最佳答案
您的 Account
模型中的
validates_presence_of :users
是测试失败的原因。您的帐户中至少需要一位用户,才能创建。
我不确定你到底想做什么,所以我给你两种方法来解决这个问题。第一个选择是改变你的工厂:
Factory.define :account do |a|
a.name { Faker::Company.name }
a.users {|u| [u.association(:user)]}
end
Factory.define :user do |u|
u.email { Faker::Internet.email }
end
另一种方法是检查属于方的关联是否存在。所以你需要像这样改变你的模型:
class Account < ActiveRecord::Base
has_many :users
accepts_nested_attributes_for :users
end
class User < ActiveRecord::Base
belongs_to :account
validates_presence_of :account
end
关于ruby-on-rails - 工厂女孩 : association problem testing model which has validates_presence_of accepts_nested_attributes_for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3109505/
我尝试在 rails4 中创建一对多连接。但是,虽然我没有收到错误消息,但未存储嵌套属性。 我究竟做错了什么? 站型 class Station
我有 2 个模型之间的关联,Business 和 Address。企业有注册地址的地方。我这样做了如下。 class Business "Address", :foreign_key => :bus
我正在尝试将 user_id 添加到由父 Controller 构建的嵌套属性,但它似乎没有达到预期的效果? 即。我有一个名为 Place.rb 的模型,它 accepts_nested_attrib
我有一对多的关系: class Programa :restrict accepts_nested_attributes_for :roles ... end class Role p
我正在尝试通过嵌套模型保存图像 **型号: Listing has_many:photos accepts_nested_attributes_for :photos, :allow_
我的模型如下所示: class Template false base.nested_attributes_keys = {} end alias_metho
我正在使用 Rails 2.3.8 并接受_nested_attributes_for。 我有一个简单的类别对象,它使用 awesome_nested_set 来允许嵌套类别。 对于每个类别,我想要一
我最近将我的应用程序从 v4.2.0 升级到了 v4.2.4。事实证明,嵌套属性的保存顺序已更改。有没有办法定义先保存哪个“accepts_nested_attributes_for”。我能够注意到变
给定某种事物: class Thing { //...other stuff... "custom_field_values_attributes"=>{ "0"=>{ "
我正在尝试使用 accepts_nested_attributes 来创建一个复杂的表单。基于Nested Attributes文档,examples等,我已经像这样设置了模型: 用户模型: requ
所以我对 Rails 还很陌生,所以我可能遗漏了一些非常直接的东西.. 我想做的是在创建 Band 时创建一个 Artist。 模型 乐队.rb class Band
我有一个模型 Post哪个belongs_to一Section .有两个不同的 Section子类,我使用 STI 为每个子类实现不同的行为。在Post我希望每个 Section 都有一个选项卡.该选
我有 class Profile has_many :favorite_books, :dependent => :destroy has_many :favorite_quotes, :de
我有以下模型的下表: 用户(id, role_id) has_many :entries 类别(id, category_name) has_many :entries 条目(id, category
我有以下三个模型(Rails 2.3.8) class Outbreak :destroy has_many :locations, :through => :inciden
我可能完全混淆了两者,但我看到表单可以使用基于嵌套路由的数组参数促进关联,例如: <%= form_for [@project, @task]... 或使用 fields_for如果是父类的助手 ac
我有 3 个模型和它们的关联 class User true end class Player :rolable end class Manager :rolable end 我开箱即用,从 r
我有一个 Post模型。我想让用户在创建/更新帖子时创建帖子评论 accepts_nested_attributes_for :comments .但是,我不想让用户通过嵌套属性更新评论。 有没有办法
我有一个模型主题和帖子。主题 has_many :posts。 在主题模型中,我也接受了 :posts_nested_attributes_for , 似乎在使用 Post 的一些参数更新 Topic
我试图让我的用户表单也允许用户同时通过 form_for 填写他们的公司资料。出于某种原因,它没有显示公司字段。这是我的 Controller 和布局代码。 class User true end
我是一名优秀的程序员,十分优秀!