Collection_select to display 2 fields


#1

I’m trying to get a collection_select generated Drop-down box to display
two fields from a table (first name & last name); however, I am having
trouble concatenating the symbols. It appears as though any code I put
in the 5th argument for collection_select is processed prior to
executing the collection_select method. The code executes successfully
with 1 symbol in the 5th argument, but not when trying to concatenate 2.
Any help is appreciated!

My code is below:

this works:
<%=collection_select(:model_name, :field_name, @queryname, :id,
:field_name_1)%>

this doesn’t work:
<%=collection_select(:model_name, :field_name, @queryname, :id,
:field_name_1 & :field_name_2 )%>


#2

Ryan wrote:

executing the collection_select method. The code executes successfully
with 1 symbol in the 5th argument, but not when trying to concatenate 2.
Any help is appreciated!

Hey Ryan,

Yeah, that’s not gonna work. The helper is using object.send to call a
method by using the symbol you pass it. I’m new to this, but try
defining a new single method on your object which returns the
concatenated string. Pass a symbol for that new method to
collection_select.

Can someone else comment on this approach?

Dan


#3

I’ve figured out how to do this, but I’m not sure it’s 100%.

in the View:
<%=collection_select(:model_name, :field_name, @queryname, :id,
:translationClass)%>

in the Model:
composed_of :translationClass,
:mapping =>
[
[:field_name1, :field1 ],
[:field_name2, :field2 ],
[‘nil’]
]

The display in the Drop-down is the contents of field_name1 &
field_name2, which is what I was trying to accomplish.

The [‘nil’] is the one I question. If I don’t insert a 3rd element in
the Array, I get a message saying: wrong number of arguments (2 for 3)

So, I add it, and it works well. I have a hunch this is a type of
workaround… but it does work.


#4

On Thu, 2006-04-27 at 18:43 +0200, Ryan wrote:

this works:
<%=collection_select(:model_name, :field_name, @queryname, :id,
:field_name_1)%>

this doesn’t work:
<%=collection_select(:model_name, :field_name, @queryname, :id,
:field_name_1 & :field_name_2 )%>


see Aggregations which allows you to join to columns and use them at
once.

Craig