在Many2Many模型中,View / show具有未知列

时间:2013-02-12 20:49:46

标签: ruby-on-rails model-associations

我以为我可以通过系统地移动选项来打败这个。 它赢了。

index.html.erb和form.html.erb就像现在一样。

我在出价和联系人之间存在多对多关系,其中bid_contacts介于该表格中的bid_id和customer_contact_id之间。

以下是联系人然后出价模型:

has_and_belongs_to_many :bid_customer_contacts, :class_name => 'Bid',
  :association_foreign_key => 'bid_id'

has_and_belongs_to_many :customer_contacts, :class_name => 'Contact', :foreign_key => 'customer_contact_id'

bid_controller.rb没什么特别的

show.html.erb文件错误:

Mysql2::Error: Unknown column 'bids_contacts.contact_id' in 'on clause':   
SELECT  `contacts`.* FROM `contacts` INNER JOIN `bids_contacts'  
    ON  `contacts`.`id` = `bids_contacts`.`contact_id'  
 WHERE  'bids_contacts`.`customer_contact_id` = 15

代码是:

<b>Customer Contacts:</b>
<% if !@bid.customer_contacts.empty? %>   <<===============
  <ul>
    <% @bid.contacts.each do |bc| %>
        <li><%= link_to(bc.name, bc) %></li>
    <% end %>
</ul>
<% else %>
    No Customer Contacts<br/>
<% end %>

我在M2M表中尝试了 contact_id ,而不是 customer_contact_id ,但我只是遇到了不同的错误。

让我知道是否需要分享其他东西才能解开这个谜团。 感谢。

1 个答案:

答案 0 :(得分:0)

在您看来,您正在致电

@bid.customer_contacts

然后再

@bid.contacts

你可能想要

@bid.customer_contacts

这两次,因为您使用

以这种方式定义了方法
has_and_belongs_to_many :customer_contacts ...

我假设您将customer_contact_id作为数据库中的字段,但是当您调用.contacts时,您的视图会查找不存在的contact_id字段。