2013-11-22 89 views
0

我有3種型號:無法顯示選擇框

貸款
設備
類別

貸款belongs_to的設備

設備belongs_to的:類
設備的has_many:身外之物

類別has_many:設備

我想用貸款的形式顯示一個選擇框,但貸款和類別模型之間沒有任何關聯。我無法看到這兩個模型(貸款和類別)之間的關聯,但設備和類別是。 我該怎麼做?我正在使用簡單的表單! 請原諒我的英語!

回答

0

已經生成的has_many :through一個典型例子:

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

一個的has_many:通過協會經常被用來建立與其他模型中的許多一對多連接。該關聯表明聲明模型可以通過繼續第三個模型與另一個模型的零個或多個實例進行匹配。例如,考慮患者預約看醫生的醫療實踐。相關的協會聲明可能如下所示:

class Physician < ActiveRecord::Base 
    has_many :appointments 
    has_many :patients, through: :appointments 
end 

class Appointment < ActiveRecord::Base 
    belongs_to :physician 
    belongs_to :patient 
end 

class Patient < ActiveRecord::Base 
    has_many :appointments 
    has_many :physicians, through: :appointments 
end 
0

在這種情況下,您應該使用委派。將此行添加到您的lending.rb文件中。

delegate :category, to: :equipment 這將返回有關的類別。