Forum: Ruby on Rails collection_select to display 2 fields

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6a867819a874b2a6d0a06200a70cdfa5?d=identicon&s=25 Ryan (Guest)
on 2006-04-27 18:43
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  )%>
49c38b11918618294f164df6cd9d84c1?d=identicon&s=25 Dan Perez (perezda)
on 2006-04-27 19:51
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
6a867819a874b2a6d0a06200a70cdfa5?d=identicon&s=25 Ryan (Guest)
on 2006-04-27 23:18
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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-28 15:04
(Received via mailing list)
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
This topic is locked and can not be replied to.