Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 324447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:07:30+00:00 2026-05-12T09:07:30+00:00

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

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:07:30+00:00Added an answer on May 12, 2026 at 9:07 am

    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…!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two large identical-sized files. One is ASCII plain text, and the other
I have two identical strings, one in an array and one in a String
I have two identical tables, one with current values for rows, and one with
I have two identical tables and need to copy rows from table to another.
I have two identical byte arrays in the following segment of code: /// <summary>
I have two almost identical Beans. Bean Item contains a unique identifier (primary key),
I have two schemas S1 and S2 with identical table structure. I`ll start with
I have two websites that have an almost identical database schema. the only difference
I have two very simple, identical UITableViews in my app that are populated with
Suppose I have two documents that are identical except the lines are shuffled. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.