问题描述
||
我已使用CRUD创建联系人和创建群组。两者都嵌套在用户模型下。
我需要知道如何将联系人与群组相关联。
我希望在我的联系表单中有一些复选框(使用formtastic),以便用户可以选择联系人所属的组。
在php中,我将创建一个名为contact_to_groups的表,并且我将具有contact_id和group_id列,然后当我保存联系人时,我将传递该数据并使用联接将其取回。
谢谢!
联系创建表格
<%= semantic_form_for [@contact.user,@contact] do |f| %>
<% f.inputs do %>
<%= f.input :firstname,:label => \'First Name\' %>
<%= f.input :lastname,:label => \'Last Name\' %>
<%= f.input :email,:label => \'Email\' %>
<%= f.input :notes,:input_html => { :class => \'autogrow\',:rows => 10,:cols => 50,:maxlength => 10 },:label => \'Notes\' %>
<% end %>
<%= f.buttons %>
<%结束%>
解决方法
像这样更正您的模型:
class Group < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :contacts
end
class Contact < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :groups
end
然后您需要在数据库contacts_groups(contact_id,group_id)中创建表