0
我有3種型號:導軌 - 進入HABTM中間人的關係數據
#Client.rb
has_many :contracts
has_many :accessories, :through => :contracts
#Contract.rb
has_and_belongs_to_many :accessories
belongs_to :client
#Accessory.rb
has_and_belongs_to_many :contracts
belongs_to :pay_interval
has_many :clients, :through => :contracts
在我的客戶的show.html.erb
視圖頁上,我想列出所有客戶的配件,合同編號附件屬於。例如:
<% @client.accessories.each do |a| %>
<%= a.name %>
<%= a.contract.id %>
<% end %>
然而,contract
無法通過附件進行訪問,只有合同(複數),但我想,以避免第二個循環。那我該怎麼做?
你模型模式說'附件'屬於**許多**合同;它沒有單個':contract_id'。 – deefour
我指出了這一點,因爲'a.contract.id'在附件的上下文中沒有意義,因爲根據您的模型關聯沒有單個'contract'。 – deefour
是的,我明白這一點。我只是想知道是否有一種方法可以防止我想要做的循環內部有循環。 –