Forum: Ruby on Rails How do I create a collection

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2007-05-15 23:02
Hopefully this isn't a dumb question.

I would like to create a drop down list using collection_select, but I
would like the collection to be generate statically, and not from a
database call.

If I do create the collection from a database table call, the output
using inspect looks like this:

[#<Carrier:0x3479338 @attributes={"name"=>"FedEx", "enabled"=>"1",
"id"=>"1"}>, #<Carrier:0x34792fc @attributes={"name"=>"USPS",
"enabled"=>"1", "id"=>"2"}>]

How do I recreate an array like this without using activerecord and a
database to do it?

To me it looks like an array of hashes, but I don't quite understand
where the Carrier @attributes part comes from.

Any help is appreciated. Seems like something so simple, I just cant
seem to get it.

21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-15 23:16
(Received via mailing list)
Here's one way to create a collection:

words = %w{ this is a collection of some words }

Here's another (This one is an empty collection):

objects = []

Now you can use the << method to add stuff to your collection:

object << my_object

On May 15, 5:02 pm, David Coleman <>
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-15 23:27
(Received via mailing list)
But, seriously...

Take a look at:

options_for_select(container, selected = nil)
Accepts a container (hash, array, enumerable, your type) and returns a
string of option tags. Given a container where the elements respond to
first and last (such as a two-element array), the "lasts" serve as
option values and the "firsts" as option text. Hashes are turned into
this form automatically, so the keys become "firsts" and values become
lasts. If selected is specified, the matching "last" or element will
get the selected option-tag. Selected may also be an array of values
to be selected when using a multiple select.

Examples (call, result):
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-15 23:34
(Received via mailing list)
As a side note you don't necessarily need collection_select for
dealing with a static list.

Take a look a the rails documentation for:
select(object, method, choices, options = {}, html_options = {})

which will in turn direct you to the previous post for the format of
This topic is locked and can not be replied to.