ruby on Rails、factory_girl、validates_presence_of 和多态关联
情况是这样的。
Gems:rails 3.2、factory_girl 2.5.1
class House
has_one :address, :as => :addressable
validates :address, :presence => true
accepts_nested_attributes_for :address
end
class Address
attr_accessor :nested
belongs_to :addressable, :polymorhic => true
validates :addressable, :presence => true, :unless => :nested
end
这是如何工作的。
<%= form_for @house do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :address do |a| %>
<%= a.hidden_field :nested %>
<%= a.label :street_address %>
<%= a.text_field :street_address %>
定义工厂的正确方法是什么?
# does not work
Factory.define :house do |h|
h.association :address
end
# does not work
Factory.define :house do |h|
h.after_build do |record|
Factory.build(:address, :addressable => record, :nested => '')
end
end
# does not work
Factory.define :house do |h|
h.after_build do |record|
Factory.create(:address, :addressable => record, :nested => '')
end
end
所以基本上,允许accepts_nested_attributes_for :address绕过验证并同时创建两条记录的“技巧”在factory_girl中不起作用。目前,这种丑陋的混乱是唯一的解决办法。
home = House.new
home.name = 'On the prairie'
home.address_attributes = Factory.attributes_for(:address, :nested => '')
home.save
更新 解决方案:
Factory.define :house do |h|
h.after_build do |record|
record.address = Factory.build(:address, :addressable => record)
end
end
Here's the situation.
Gems: rails 3.2, factory_girl 2.5.1
class House
has_one :address, :as => :addressable
validates :address, :presence => true
accepts_nested_attributes_for :address
end
class Address
attr_accessor :nested
belongs_to :addressable, :polymorhic => true
validates :addressable, :presence => true, :unless => :nested
end
How this works.
<%= form_for @house do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :address do |a| %>
<%= a.hidden_field :nested %>
<%= a.label :street_address %>
<%= a.text_field :street_address %>
What is the correct way to define a factory?
# does not work
Factory.define :house do |h|
h.association :address
end
# does not work
Factory.define :house do |h|
h.after_build do |record|
Factory.build(:address, :addressable => record, :nested => '')
end
end
# does not work
Factory.define :house do |h|
h.after_build do |record|
Factory.create(:address, :addressable => record, :nested => '')
end
end
So basically, the 'trick' that allows accepts_nested_attributes_for :address to get around the validations and create both records at the same time is not working in factory_girl. Currently, this ugly mess is the only solution.
home = House.new
home.name = 'On the prairie'
home.address_attributes = Factory.attributes_for(:address, :nested => '')
home.save
UPDATE
Solution:
Factory.define :house do |h|
h.after_build do |record|
record.address = Factory.build(:address, :addressable => record)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第二次 FactoryGirl 尝试已接近完成,但您需要使用该构建地址执行某些操作。
Your second FactoryGirl attempt is close, but you need to do something with that built address.