views:

299

answers:

1

hi all,

i have two identical collection_selects on one page (one message belonging to 2 groups)

<%= 
collection_select(:message,:group_ids, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} ) 
%>
<%= 
collection_select(:message,:group_ids, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} ) 
%>

is it possible to set two different selected values for them using collection_select?

edit:

i guess i'd have to do something like

<%
@message.group_id=5
%>
<%= 
collection_select(:message,:group_id, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} ) 
%>
<%
@message.group_id=6
%>
<%= 
collection_select(:message,:group_id, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} ) 
%>

but of course it doesn't work and gives method missing error

edit2:

guess there is no way to do it with collection_select. unless group has a method, returning single group_id each time.

what i ended up with is

 select_tag 'message[group_ids][]', "<option></option>"+options_from_collection_for_select(Group.find(:all), 'id', 'title',group1.id)
 select_tag 'message[group_ids][]', "<option></option>"+options_from_collection_for_select(Group.find(:all), 'id', 'title',group2.id)
+2  A: 

You need to set up your models and relationships like so:

class Message < ActiveRecord::Base  
  has_many :message_groups   
  has_many :groups, :through => :message_groups  
  accepts_nested_attributes_for :message_groups  #Note this here! 
end

class Group < ActiveRecord::Base
  has_many :message_groups
  has_many :messages, :through => :message_groups
end

class MessageGroup < ActiveRecord::Base
  belongs_to :group
  belongs_to :message
end

Then in your form...

<% form_for(@message) do |f| %>
  <%= f.error_messages %>
    <% f.fields_for :message_groups do |g| %>
    <p>
     <%= g.label :group_id, "Group" %>
     <%= g.select :group_id, Group.find(:all).collect {|g| [ g.title, g.id ] } %>
    </p>
    <% end %>
  <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>

And here's my migrations for completeness

class CreateGroups < ActiveRecord::Migration
  def self.up
    create_table :groups do |t|
      t.string :title
      t.timestamps
    end
  end

  def self.down
    drop_table :groups
  end
end

class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.text :body
      t.timestamps
    end
  end

  def self.down
    drop_table :messages
  end
end


class CreateMessageGroups < ActiveRecord::Migration
  def self.up
    create_table :message_groups do |t|
      t.integer :message_id
      t.integer :group_id
      t.timestamps
    end
  end

  def self.down
    drop_table :message_groups
  end
end

Hope this helps...!

Chalkers
Looks like a has_and_belongs_to_many relationship to me. Also, isn't the migration meant to read "create_table :message_groups :id=> false do |t|" or you will get funky errors?
askegg
Stratch that. :id => false is only valid for HABTM relations. The above code is cool.
askegg
Thank you askegg :D
Chalkers
thanks! mainly for accepts_nested_attributes_for, didn't know about this one.
Pavel K.