【问题标题】:Factory Girl with polymorphic has_many association具有多态 has_many 关联的 Factory Girl
【发布时间】:2013-05-01 04:35:11
【问题描述】:

我正在尝试使用新语法建立一个带有多态关联的 factorygirl has_many 关联。这是代码。它没有正确构建地址并将其与站点关联。

class Address < ActiveRecord::Base

  belongs_to :addressee, :polymorphic => true

end

class Site < ActiveRecord::Base

  has_many addresses, :as => :addressee, :dependent => :destroy

end

***** FACTORYGIRL ******


FactoryGirl.define do
    factory :address, class: 'Address' do
        property_type "House"
        street_number "31"
        street_type "STREET"
        suburb "XXXXX"
        postcode "XXXX"
    end

    factory :site_address, class: 'Address' do
        property_type "House"
        street_number "31"
        street_type "STREET"
        suburb "XXXX"
        postcode "XXXX"
        site
    end
end


FactoryGirl.define do

    factory :site, class: 'Site' do
        name "MyString"
        organisation_category nil
        service_type_organisation_category_id 1
        telephone "MyString"
        fax "MyString"
        email "MyString"
        url "MyString"
        clinical_software_package_id 1
        billing_software_package_id 1
        bulk_billing false
        disabled_access false
        ncacch false
        parking false
        organisation nil

        factory :site_with_addresses do
            ignore do
                address_count 1
            end

            after (:create) do |site,evaluator|
                FactoryGirl.create_list(:site_address, evaluator.address_count, addressee: site)
            end
        end

    end


end

【问题讨论】:

    标签: ruby-on-rails ruby rspec ruby-on-rails-3.2 factory-bot


    【解决方案1】:

    你还没有在工厂内声明关联地址,试试

    factory :address, class: 'Address' do
        property_type "House"
        street_number "31"
        street_type "STREET"
        suburb "XXXXX"
        postcode "XXXX"
        association :addressee, :factory => :site
    end
    

    有关如何自定义关联的更多详细信息,请参阅https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations

    【讨论】:

    • 我猜这行不通。因为他没有工厂叫addresseeIf the factory name is the same as the association name
    • 大家好,gist.github.com/emilevictor/a376231cf11345b35e1b 这是我的网站工厂的 factorygirl 资源。我以为我已经包含了它,但我不小心包含了 :address 工厂。相关代码是:site_address 和:site。我当前使用的方法(如网站上所示)是错误的。
    • 这对我来说非常有效。 FactoryGirl 似乎理解 :addressee 是一个多态关联,并且它正确设置了 Address 对象的 :addressee_id:addressee_type 属性。
    【解决方案2】:

    这个要点可能会有所帮助 - https://gist.github.com/travisr/2830535/

    class Alert < ActiveRecord::Base
      belongs_to :alertable, :polymorphic => true
    end
    
    
    class Region < ActiveRecord::Base
      has_many :alerts, :as => :alertable
    end
    
    
    FactoryGirl.define do
      Factory.define :alert do |alert|
        alert.alertable { |a| a.association(:region) }
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      相关资源
      最近更新 更多