我有一个与公司有多对多关系的模型联系人:
class Contact < ActiveRecord::Base has_many :contactcompanies
has_many :companies, through: :contactcompanies
公司型号:
class Company < ActiveRecord::Base
has_many :contactcompanies
has_many :contacts, through: :contactcompanies
联系公司:
class ContactCompany < ActiveRecord::Base
belongs_to :company
belongs_to :contact
联系人控制器.rb:
def new
@contact = Contact.new
@all_companies = Company.all
@contact_company = @contact.contactcompanies.build
end
联系创建表格,我想在其中为公司进行多项选择:
<%= form_for @contact do |f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :image_url %>
<%= f.text_field :image_url %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= fields_for(@contact_company) do |cc| %>
<%= cc.label "All Companies" %>
<%= collection_select(:companies, :id, @all_companies, :id, :name, {}, { :multiple => true }) %>
<% end %>
<div class="form-action">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
contacts_path, :class => 'btn' %>
</div>
<% end %>
我的问题是当我转到 /contacts/new 时出现此错误:自动加载常量 Contactcompany 时检测到循环依赖
有任何想法吗?我正在寻找几个小时没有成功。谢谢