2015-10-19 110 views
0

我有三種模型。模型通過多重屬性嵌套屬性到

class Business < ActiveRecord::Base 
    has_one :employee 

    accepts_nested_attributes_for :employee 
end 

class Employee < ActiveRecord::Base 
    has_one :slave 

    belongs_to :business 

    accepts_nested_attributes_for :slave 
end 

class Slave < ActiveRecord::Base 
    belongs_to :employee 
end 

我有一些奴隸參數被髮送到我的控制器更新奴隸和商業記錄。從屬參數由一個密鑰business_attributes組成,並帶有更新的業務屬性。不過,我不確定如何正確連接它。

我應該在從屬模型中添加accepts_nested_attributes_for以接受business_attributes?或者我需要在employee_attributes散列碼中包含business_attributes,並且僱員模型accepts_nested_attributes_forbusiness_attributes?任何幫助,將不勝感激。

回答

0

首先我會說你可能會過度複雜你的結構,或者如果你需要以這種方式傳遞數據的話。您可以使用業務控制器來使用您已有的關係來處理表單邏輯。

如果您絕對需要將業務屬性直接傳遞給從屬設備,則需要在從屬設備和業務之間直接創建某種關係。喜歡的東西:

delegate :business, to: :employee 
accepts_nested_attributes_for :business 

*注:這是未經測試,並有望成爲沒有效率,如果你經常從從(N + 1查詢)引用業務。