2012-02-26 18 views
2

我有以下型號Rails的主動管理多對多關係

class Owner < ActiveRecord::Base 
    has_many :business_owners 
    has_many :businesses, :through => :business_owner 
end 

class Business < Active Record::Base 
    has_many :business_owners 
    has_many :businesses, :through => :business_owner 
end 

class BusinessOwner < ActiveRecord::Base 
    belongs_to :owners 
    belongs_to :businesses 
end 

我已經嘗試了BusinessOwner模型添加到管理儀表板都:在應用

rails generate active_admin:resource BusinessOwner 

它創建了一個文件名爲business_owners/admin/ 當我嘗試查看業主我收到以下錯誤:

uninitialized constant BusinessOwner::Owners 
Extracted source (around line #1): 

1: render renderer_for(:index) 

有人能告訴我如何使用活動管理與多對多的關係?

回答

2

你的關係看起來不正確。

belongs_to關係,Rails的希望你用單數形式

class BusinessOwner < ActiveRecord::Base 
    belongs_to :owner 
    belongs_to :business 
end 

同樣,你需要正確地引用:through

has_many :businesses, :through => :business_owners 

(即複數業主)

這是通常值得引發Rails控制檯(或者WRITING TESTS,嘿嘿)在你考慮ActiveAdmin之前測試這些東西;)

+0

您的岩石。我的不好。是的,一個或兩個測試會發現這一點。 – 2012-02-26 15:14:39

2

在經營業務表正確的做聯想這樣的:

class Business < Active Record::Base 
    has_many :business_owners 
    has_many :oweners, :through => :business_owner 
end