# Form select box from association

**URL:** <https://www.ruby-forum.com/t/form-select-box-from-association/88682>\
**Category:** Rails\
**Created:** [March 13, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/form-select-box-from-association/88682 "2007-03-13T15:03:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 13, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/form-select-box-from-association/88682/1 "2007-03-13T15:03:21Z")

</div>

Hello there,

I have the following models

class Bibliography \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :gthemes, :join\_table =\>  
“gthemes\_bibliographies”  
end

class Gtheme \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :bibliographies, :join\_table =\>  
“gthemes\_bibliographies”  
end

I want to include in the bibliography edit page a select box with all  
the gthemes and i want the members of @bibliography.gthemes to be  
marked as selected

I 'm using this in bibliography/\_form.rhtml:

\<%= select(‘bibliography’, ‘gthemes’ , Gtheme.find(:all).collect{ |gt|  
[gt.name, gt.id]}, {}, {:multiple =\> true}) %\>

but it doesn’t mark the current gthemes as selected, any ideas?

Thanks in advance,  
Christos

---

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 13, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/form-select-box-from-association/88682/2 "2007-03-13T16:47:43Z")

</div>

On Mar 13, 4:02 pm, “Trochalakis C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> has\_and\_belongs\_to\_many :bibliographies, :join\_table =\>  
> [gt.name, gt.id]}, {}, {:multiple =\> true}) %\>
> 
> but it doesn’t mark the current gthemes as selected, any ideas?
> 
> Thanks in advance,  
> Christos

I also tried using the form\_for helper but the problem still exists

\<% form\_for :bibliography, @bibliography, :url =\> { :action =\>  
“update” } do |f| %\>  
Bgthemes: \<%= f.select :gthemes, Gtheme.find(:all).collect {|c|  
[c.name, c.id]}, {}, {:multiple =\> true} %\>  
\<%= submit\_tag ‘Save changes’ %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 20, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/form-select-box-from-association/88682/3 "2007-03-20T19:55:26Z")

</div>

On Mar 13, 5:46 pm, “Trochalakis C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > has\_and\_belongs\_to\_many :gthemes, :join\_table =\>  
> > marked as selected
> 
> I also tried using the form\_for helper but the problem still exists
> 
> \<% form\_for :bibliography, @bibliography, :url =\> { :action =\>  
> “update” } do |f| %\>  
> Bgthemes: \<%= f.select :gthemes, Gtheme.find(:all).collect {|c|  
> [c.name, c.id]}, {}, {:multiple =\> true} %\>  
> \<%= submit\_tag ‘Save changes’ %\>  
> \<% end %\>

For the record, I didn’t manage to find a “magic way” to do it, but  
this is a nice and working solution:

\<%= select\_tag ‘gthemes[]’,  
options\_from\_collection\_for\_select(@gthemes, ‘id’, ‘name’,  
@bibliography.gtheme\_ids ),  
{:multiple =\> true, :class =\> “wide”}  
%\>

---

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/form-select-box-from-association/88682/4 "2007-09-25T23:05:48Z")

</div>

On Mar 20, 8:30 pm, “Trochalakis C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> For the record, I didn’t manage to find a “magic way” to do it, but  
> this is a nice and working solution:
> 
> \<%= select\_tag ‘gthemes[]’,  
> options\_from\_collection\_for\_select(@gthemes, ‘id’, ‘name’,  
> @bibliography.gtheme\_ids ),  
> {:multiple =\> true, :class =\> “wide”}  
> %\>

@gthemes = Gtheme.find(:all)
