# Select helper

**URL:** <https://www.ruby-forum.com/t/select-helper/176287>\
**Category:** Rails\
**Created:** [September 28, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/select-helper/176287 "2009-09-28T16:17:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 28, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/select-helper/176287/1 "2009-09-28T16:17:34Z")

</div>

How do I amend the form helper:

\<%= select(“lead”, “associate\_id”, Associate.all.collect {|p|  
[p.to\_s, p.id] }, {:include\_blank =\> ‘’}) %\>

(which is magnificently SIMPLE, and I don’t want to convolute it here,  
which is why I am asking the experts on this list…)

To NOT display records where Associate.deleted != 0 ?

Additionally, how would I amend it if I have and Associate.id that I  
want to be selected in it? Thanks guys.

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 28, 2009, 4:46pm UTC](https://www.ruby-forum.com/t/select-helper/176287/2 "2009-09-28T16:46:16Z")

</div>

2009/9/28 RVince [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> How do I amend the form helper:
> 
> \<%= select(“lead”, “associate\_id”, Associate.all.collect {|p|  
> [p.to\_s, p.id] }, {:include\_blank =\> ‘’}) %\>
> 
> (which is magnificently SIMPLE, and I don’t want to convolute it here,  
> which is why I am asking the experts on this list…)
> 
> To NOT display records where Associate.deleted != 0 ?

You could provide a named scope of Associate that finds records where  
deleted != 0 (called active or whatever is right for you) then use  
Associate.active instead of Associate.all.  
In fact I would also provide a method of Associate that returns the  
collected data, then it becomes even simpler  
\<%= select(“lead”, “associate\_id”, Associate.options\_for\_select,  
{:include\_blank =\> ‘’}) %\>  
This also makes testing simpler as you can test options\_for\_select in  
a unit test.

> Additionally, how would I amend it if I have and Associate.id that I  
> want to be selected in it? Thanks guys.

Sorry I don’t understand the question

Colin

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 28, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/select-helper/176287/3 "2009-09-28T22:02:23Z")

</div>

Suppose I have the following then in my view – I am creating a form  
for the model ‘Lead’ and there is a field lead.associate\_id. In my  
controller, I am setting a variable to a model @Up, and @Up has an  
associate\_id also. I would like the default lead.associate\_id be set  
in my select with the @Up.associate\_id. Please note the second line in  
my view code below (which does not work, of course). How can I default  
the lead.associate\_id on this form to create a new lead to be equal to  
the @Up.associate\_id ?

\<% form\_for :leads, :url =\> { :controller =\> ‘channels’, :action =\>  
“lead” }, :html =\> {:name =\> ‘leadform’} do |f| %\>  
\<% lead.associate\_id = @Up.associate\_id %\>  
\<%= select(“lead”, “associate\_id”, Associate.find(:all, :order =\>  
‘associates.lastname ASC’, :conditions =\> [‘deleted=0’]).collect {|p|  
[p.to\_s, p.id]}, {:include\_blank =\> ‘’}) %\>

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 28, 2009, 10:18pm UTC](https://www.ruby-forum.com/t/select-helper/176287/4 "2009-09-28T22:18:46Z")

</div>

2009/9/28 RVince [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> \<% form\_for :leads, Â :url =\> { :controller =\> ‘channels’, :action =\>  
> I would expect this to be :lead (singular)

> “lead” }, :html =\> {:name =\> ‘leadform’} do |f| %\>  
> Â \<% lead.associate\_id = @Up.associate\_id %\>

Should this be @lead.associate\_id = @Up.associate\_id?

> \<%= select(“lead”, “associate\_id”, Associate.find(:all, Â :order =\>  
> ‘associates.lastname ASC’, Â :conditions =\> [‘deleted=0’]).collect {|p|  
> [p.to\_s, p.id]}, {:include\_blank =\> ‘’}) %\>

If you are using form\_for why are you not using f.select?

Colin

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 29, 2009, 10:01am UTC](https://www.ruby-forum.com/t/select-helper/176287/5 "2009-09-29T10:01:53Z")

</div>

2009/9/29 RVince [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> However, if I add in :
> 
> \<% @lead.associate\_id = @Up.associate\_id %\>
> 
> Nothing happens ? Additionally, I try making this statement in te  
> method of the controller which renders the partial that this view code  
> is in, again, nothing happens (i.e. it does not select the associate  
> in the select box)

I use :associate\_id in f.select but I don’t think that should make any  
difference.  
Have you verified just before the f.select that @lead.associate\_id is  
a value from the set? Display the id on the form and check the html  
(View, Page Source or similar in browser) to check that the id is in  
the selection set.  
Try it without the include\_blank option (clutching at straws here).

Colin

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 30, 2009, 1:14am UTC](https://www.ruby-forum.com/t/select-helper/176287/6 "2009-09-30T01:14:46Z")

</div>

I see where the problem is arising (but not its solution). My form is  
in a div that is hidden. My controller method is called via a  
javascript function, which also invoked the form (which is in a  
partial, in a div, re-rendering the partial) and the javascript, after  
calling the controller method, invokes a redbox on the div/partial  
with the form. This is causing a big problem here.

I must set the selected value for the select box in my javasript  
method I believe, before invoking redbox. I will post this to the  
appropriate javascript forum. Thanks for all of yoiur help on this  
knotty problem! -RVince

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 29, 2009, 2:10am UTC](https://www.ruby-forum.com/t/select-helper/176287/7 "2009-09-29T02:10:01Z")

</div>

Whoops! You’re right!

\<% form\_for :lead, :url =\> { :controller =\> ‘channels’, :action =\>  
“lead” }, :html =\> {:name =\> ‘leadform’} do |f| %\>  
\<%= f.label :origin %\>  
  
\<%= f.select( “associate\_id”, Associate.find(:all, :order =\>  
‘associates.lastname ASC’, :conditions =\> [‘deleted=0’]).collect {|p|  
[p.to\_s, p.id]}, {:include\_blank =\> ‘’}) %\>

However, if I add in :

\<% @lead.associate\_id = @Up.associate\_id %\>

Nothing happens ? Additionally, I try making this statement in te  
method of the controller which renders the partial that this view code  
is in, again, nothing happens (i.e. it does not select the associate  
in the select box)
