开发者

Php to Rails - Rails Associations - contact_to_groups table

I have CRUD in place for creating contacts and creating groups. Both are nested under the user model.

I need to know how I can now associate contacts with groups.

I would like in my contact form to have some checkboxes (using formtastic) so the user can select which group(s) the contact belongs to.

In php i would make a ta开发者_JAVA百科ble called contacts_to_groups and i would have contact_id & group_id columns, then when I would save the contact i would pass that data and use a join to get it back out later.

Thanks!

contact create form

<%= 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 %>

<% end %>


Correct your models like this:

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

And then you need to create table in DB contacts_groups(contact_id, group_id)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜