class Patient < ActiveRecord::Base
include Addressable
end
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
end
和可解决的问题:
module Addressable
extend ActiveSupport::Concern
included do
has_one :address, as: :addressable, dependent: :destroy
delegate :to_formatted_s, to: :address, prefix: true
accepts_nested_attributes_for :address
end
end
当我将此代码移至关注时,我的测试失败了:
Failure/Error: let(:patient) { create(:patient, :male, fullname: "Patient", pesel: "12345678901") }
NoMethodError:
undefined method `address=' for #<Patient:0x0000000550bb58>
我的工厂:
FactoryGirl.define do
factory :patient do
sequence(:fullname) { |n| "Patient #{n}" }
pesel "12345678901"
birth_date "2014-12-16"
address
trait :male do
sex "male"
end
trait :female do
sex "female"
end
end
end
FactoryGirl.define do
factory :address do
sequence(:street) { |n| "Street #{n}" }
sequence(:city) { |n| "City #{n}" }
sequence(:zip_code) { |n| " 12-41#{n}" }
end
end
在我将此代码置于关注之前,一切都按预期工作。为什么现在失败?
答案 0 :(得分:2)
这是一个命名冲突。我将关注名称更改为AddresableConcern,现在eveyrthing按预期工作。