我有一个与公司有多对多关系的模型联系人:
class Contact < ActiveRecord::Base has_many :contactcompanies
has_many :companies, through: :contactcompanies
公司型号:
class Company < ActiveRecord::Base
has_many :contactcompanies
has_many :contacts, through: :contactcompanies
ContactCompany:
class ContactCompany < ActiveRecord::Base
belongs_to :company
belongs_to :contact
contacts_controller.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时检测到循环依赖性
有什么想法吗?我没有成功地搜索了几个小时。感谢
答案 0 :(得分:0)
您已将您的课程声明为“ContactCompany”
这意味着:
has_many :contact_companies
has_many :contacts, through: :contact_companies
没有下划线,它正在寻找一个名为Contactcompany的类,它不存在。