Php to Rails-Rails关联-contact_to_groups表

问题描述

|| 我已使用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)中创建表     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...