2015-04-14 70 views
0

我無法讓我圍繞着如何做工廠女孩該協會頭..多工廠女孩的關聯問題

所以基本上,機構有很多代理商,業主和屬性。當我檢查我的房產工廠是否有效時,它返回錯誤說客戶經理和房東不能爲空。我如何創建這些關聯?當創建一個財產時,它將'代理'關聯起來並創建其中的一個,但我需要'業主'和'代理'也是'代理'的組織。

部分問題可能是,一直盯着這個這麼長時間,所以很抱歉,如果它是直截了當的!

型號:

class Agency < ActiveRecord::Base 
    has_many :agents 
    has_many :landlords 
    has_many :properties 
end 

class Property < ActiveRecord::Base 
    belongs_to :landlord 
    belongs_to :agency 
    belongs_to :account_manager, class_name: 'Agent', foreign_key: 'agent_id' 
end 

class User < ActiveRecord::Base 
    belongs_to :agency 
end 

class Agent < User 
    has_many :properties 
end 

class Landlord < User 
    has_many :properties 
end 

工廠:

FactoryGirl.define do 
    factory :property do 
    address_line_1 "13 Fake Street" 
    address_line_2 "Strange Lane" 
    town "Fake Town" 
    county "FakeCounty" 
    post_code "PA0 0WU" 
    beds 4 
    baths 2 
    agency 

    factory :invalid_property do 
     address_line_1 "" 
    end  
    end 
end 

FactoryGirl.define do  
    factory :agency do 
    name "First Agency" 
    telephone_number "01993 388388" 
    address_line_1 "1 Fake Street" 
    town "Fake Town" 
    county "FakeCounty" 
    post_code "BA1 4GG" 
    end 
end 

FactoryGirl.define do 
    factory :agent, class: User do 
    first_name "James" 
    last_name "Smith" 
    email "[email protected]" 
    password "helloworld" 
    telephone_number "01935 222333" 
    mobile_telephone_number "07382 928777" 
    agency 
    end 

    factory :landlord, class: User do 
    first_name "Bob" 
    last_name "Builder" 
    email "[email protected]" 
    password "helloworld" 
    telephone_number "01935 444777" 
    agency 

    factory :invalid_landlord, class: User do 
     first_name "" 
    end 
    end 
end 
+0

我加入基於假設業主型號,請檢查它是否可以 –

+0

的,工廠指定你的''一個agency':agent'。但是這種關係不會在你的模型中出現 –

+0

謝謝你的補充。我實際上是從用戶模型中爲業主和代理模型使用STI,所以belongs_to:代理從那裏繼承:) – Advocation

回答

0

事實證明,你不能真正做到這些類型的工廠內的關係。

最後,我已經刪除了關聯,並且首先手動構建代理機構和任何其他關聯,然後將它們傳遞到工廠的構建中。例如:

let(:agency) { build(:agency) } 
    let(:account_manager) { build(:agent, agency: agency) } 
    let(:landlord) { build(:landlord, agency: agency) } 
    let(:property) { build(:property, agency: agency, account_manager: account_manager, landlord: landlord) }