ruby on Rails、factory_girl、validates_presence_of 和多态关联

发布于 2025-01-03 20:50:50 字数 1590 浏览 0 评论 0原文

情况是这样的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2025-01-10 20:50:50

您的第二次 FactoryGirl 尝试已接近完成,但您需要使用该构建地址执行某些操作。

FactoryGirl.define do

  factory :house do
    after_build do |house|
      house.address = Factory.build(:address)
    end
  end

end

Your second FactoryGirl attempt is close, but you need to do something with that built address.

FactoryGirl.define do

  factory :house do
    after_build do |house|
      house.address = Factory.build(:address)
    end
  end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文