<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Ruby on Rails:  Create a Select Drop Down from an Array</title>
	<atom:link href="http://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/feed/" rel="self" type="application/rss+xml" />
	<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/</link>
	<description>Strategic eCommerce Technology and Architecture</description>
	<lastBuildDate>Tue, 13 Mar 2012 02:18:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: Ben</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-72</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Wed, 28 Jan 2009 00:47:44 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-72</guid>
		<description>Rails 2.1+
You may want to take a look at using the Rails.cache capabilities.

In a class:
app/models/years.rb
class Years
  def self.get_years
    Rails.cache.fetch(&#039;years&#039;) {(1920..Time.now.strftime(’%Y’).to_i).entries}
  end

  def self.update_years
    if self.get_years.max &lt; Time.now.strftime(’%Y’).to_i
      Rails.cache.delete(&#039;years&#039;)
      self.get_years
    end
  end
end

use Years.get_years to return a cached version of the years.
use Years.update_years to update the cache with the current year dataset...might want to schedule updates for every night at 12:00AM.
Note that if you are running mongrel each cache is seen as a separate store unless you go with memcached...
You can get crazy with caching using memcached if you have a data set that you really want to have highly available/distributed.</description>
		<content:encoded><![CDATA[<p>Rails 2.1+<br />
You may want to take a look at using the Rails.cache capabilities.</p>
<p>In a class:<br />
app/models/years.rb<br />
class Years<br />
  def self.get_years<br />
    Rails.cache.fetch(&#8216;years&#8217;) {(1920..Time.now.strftime(’%Y’).to_i).entries}<br />
  end</p>
<p>  def self.update_years<br />
    if self.get_years.max &lt; Time.now.strftime(’%Y’).to_i<br />
      Rails.cache.delete(&#8216;years&#8217;)<br />
      self.get_years<br />
    end<br />
  end<br />
end</p>
<p>use Years.get_years to return a cached version of the years.<br />
use Years.update_years to update the cache with the current year dataset&#8230;might want to schedule updates for every night at 12:00AM.<br />
Note that if you are running mongrel each cache is seen as a separate store unless you go with memcached&#8230;<br />
You can get crazy with caching using memcached if you have a data set that you really want to have highly available/distributed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Danilo</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-71</link>
		<dc:creator>Danilo</dc:creator>
		<pubDate>Tue, 11 Nov 2008 18:22:04 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-71</guid>
		<description>Probably depends upon how much stuff you&#039;re putting in the environment.rb file.  I see why you might want to do this if you have a lot of label/values to manage.  I kind of like doing this over putting them into a database, as long as they are static in nature and read-only.

The key is striking the balance, and you&#039;ll know for sure if you&#039;ve done this if it&#039;s maintainable.</description>
		<content:encoded><![CDATA[<p>Probably depends upon how much stuff you&#8217;re putting in the environment.rb file.  I see why you might want to do this if you have a lot of label/values to manage.  I kind of like doing this over putting them into a database, as long as they are static in nature and read-only.</p>
<p>The key is striking the balance, and you&#8217;ll know for sure if you&#8217;ve done this if it&#8217;s maintainable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dara</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-70</link>
		<dc:creator>Dara</dc:creator>
		<pubDate>Tue, 11 Nov 2008 16:35:58 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-70</guid>
		<description>thanks for this guys,

nice solution, any improvements since ?

My form field entry is now:






My application wide helper is :

  def channel_select(f)
    f.collection_select(:source, CHANNEL_SELECT_COLLECTION, :label, :value, {:prompt =&gt; &#039;Select Channel Source&#039;})
  end

And my code in environment.rb is

LabelValue = Struct.new(:label, :value)
CHANNEL_SOURCES_ARRAY = [&#039;abc&#039;, &#039;def&#039;, &#039;ghi&#039;]
CHANNEL_SELECT_COLLECTION = CHANNEL_SOURCES_ARRAY.collect { &#124;chan&#124; LabelValue.new(chan,chan) }


I am cautiosly wary about editing such code into environment.rb, might move the array content out specific env files, or just put something into /lib

any thoughts ?</description>
		<content:encoded><![CDATA[<p>thanks for this guys,</p>
<p>nice solution, any improvements since ?</p>
<p>My form field entry is now:</p>
<p>My application wide helper is :</p>
<p>  def channel_select(f)<br />
    f.collection_select(:source, CHANNEL_SELECT_COLLECTION, :label, :value, {:prompt =&gt; &#8216;Select Channel Source&#8217;})<br />
  end</p>
<p>And my code in environment.rb is</p>
<p>LabelValue = Struct.new(:label, :value)<br />
CHANNEL_SOURCES_ARRAY = ['abc', 'def', 'ghi']<br />
CHANNEL_SELECT_COLLECTION = CHANNEL_SOURCES_ARRAY.collect { |chan| LabelValue.new(chan,chan) }</p>
<p>I am cautiosly wary about editing such code into environment.rb, might move the array content out specific env files, or just put something into /lib</p>
<p>any thoughts ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Janus</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-69</link>
		<dc:creator>Janus</dc:creator>
		<pubDate>Thu, 16 Oct 2008 12:35:18 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-69</guid>
		<description>Please give me your full code on below, helper method and how u called it from view. And why keeping the below in environment.rb



You are the man. Here is my the pertinent code in my “environment.rb” file. I just deleted the LabelValue class entirely. It very much reminds me of the bean declarations in the Application Context, no?

# ——- Structs
LabelValue = Struct.new(:label,:value)

# ——- Constant Values

# Year array
YEARS_ARRAY = (1920..2008).entries

# Year collection
YEARS_SELECT_COLLECTION = YEARS_ARRAY.collect {&#124;yr&#124; LabelValue.new(yr,yr)}

….

From there, I just use the method in the helper file to populate the values — it’s all “static” so you get the single instance. Much, much cleaner.

Thanks</description>
		<content:encoded><![CDATA[<p>Please give me your full code on below, helper method and how u called it from view. And why keeping the below in environment.rb</p>
<p>You are the man. Here is my the pertinent code in my “environment.rb” file. I just deleted the LabelValue class entirely. It very much reminds me of the bean declarations in the Application Context, no?</p>
<p># ——- Structs<br />
LabelValue = Struct.new(:label,:value)</p>
<p># ——- Constant Values</p>
<p># Year array<br />
YEARS_ARRAY = (1920..2008).entries</p>
<p># Year collection<br />
YEARS_SELECT_COLLECTION = YEARS_ARRAY.collect {|yr| LabelValue.new(yr,yr)}</p>
<p>….</p>
<p>From there, I just use the method in the helper file to populate the values — it’s all “static” so you get the single instance. Much, much cleaner.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: crouchingbadger</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-68</link>
		<dc:creator>crouchingbadger</dc:creator>
		<pubDate>Thu, 07 Aug 2008 21:35:43 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-68</guid>
		<description>Thanks Tom &amp; Danilo, this was useful stuff as I&#039;m trying to generate a similar thing (as I&#039;m sure anyone who&#039;s trying to use rails for anything useful has)

Just wondering if it would make more sense to auto-increment the max year so you don&#039;t have to fix it with a New Years hangover once a year?

YEARS_ARRAY = (1920..Time.new.strftime(&#039;%Y&#039;).to_i).entries

Cheers
Ben</description>
		<content:encoded><![CDATA[<p>Thanks Tom &amp; Danilo, this was useful stuff as I&#8217;m trying to generate a similar thing (as I&#8217;m sure anyone who&#8217;s trying to use rails for anything useful has)</p>
<p>Just wondering if it would make more sense to auto-increment the max year so you don&#8217;t have to fix it with a New Years hangover once a year?</p>
<p>YEARS_ARRAY = (1920..Time.new.strftime(&#8216;%Y&#8217;).to_i).entries</p>
<p>Cheers<br />
Ben</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Danilo</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-67</link>
		<dc:creator>Danilo</dc:creator>
		<pubDate>Tue, 15 Jan 2008 20:41:30 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-67</guid>
		<description>Tom.  You are the man.  Here is my the pertinent code in my &quot;environment.rb&quot; file.  I just deleted the LabelValue class entirely.  It very much reminds me of the bean declarations in the Application Context, no?

  # ------- Structs
  LabelValue = Struct.new(:label,:value)

  # ------- Constant Values

  # Year array
  YEARS_ARRAY = (1920..2008).entries

  # Year collection
  YEARS_SELECT_COLLECTION = YEARS_ARRAY.collect {&#124;yr&#124; LabelValue.new(yr,yr)}

....


From there, I just use the method in the helper file to populate the values -- it&#039;s all &quot;static&quot; so you get the single instance.  Much, much cleaner.

Thanks.</description>
		<content:encoded><![CDATA[<p>Tom.  You are the man.  Here is my the pertinent code in my &#8220;environment.rb&#8221; file.  I just deleted the LabelValue class entirely.  It very much reminds me of the bean declarations in the Application Context, no?</p>
<p>  # &#8212;&#8212;- Structs<br />
  LabelValue = Struct.new(:label,:value)</p>
<p>  # &#8212;&#8212;- Constant Values</p>
<p>  # Year array<br />
  YEARS_ARRAY = (1920..2008).entries</p>
<p>  # Year collection<br />
  YEARS_SELECT_COLLECTION = YEARS_ARRAY.collect {|yr| LabelValue.new(yr,yr)}</p>
<p>&#8230;.</p>
<p>From there, I just use the method in the helper file to populate the values &#8212; it&#8217;s all &#8220;static&#8221; so you get the single instance.  Much, much cleaner.</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Danilo</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-66</link>
		<dc:creator>Danilo</dc:creator>
		<pubDate>Mon, 14 Jan 2008 17:14:31 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-66</guid>
		<description>Thanks for the comment.  I &quot;thought&quot; there was a way of creating the array this way, but couldn&#039;t find that particular constructor in any documentation ( hey, it&#039;s &quot;open source&quot;, so...) I&#039;ll definitely do that.

I looked hard at using the &quot;Struct&quot; instead of the accessors, and still might do this.  I had a worry that I might want to do &quot;more&quot; with the class at a later point, but the nice thing about Ruby is that your classes can be quite mutable without affecting their future functionality.

As far as putting the collection into a constant, you&#039;re absolutely right.  I just missed that and it would definitely be an enhancement!

Always so much to learn.  Nice comment!</description>
		<content:encoded><![CDATA[<p>Thanks for the comment.  I &#8220;thought&#8221; there was a way of creating the array this way, but couldn&#8217;t find that particular constructor in any documentation ( hey, it&#8217;s &#8220;open source&#8221;, so&#8230;) I&#8217;ll definitely do that.</p>
<p>I looked hard at using the &#8220;Struct&#8221; instead of the accessors, and still might do this.  I had a worry that I might want to do &#8220;more&#8221; with the class at a later point, but the nice thing about Ruby is that your classes can be quite mutable without affecting their future functionality.</p>
<p>As far as putting the collection into a constant, you&#8217;re absolutely right.  I just missed that and it would definitely be an enhancement!</p>
<p>Always so much to learn.  Nice comment!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom C</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/comment-page-1/#comment-65</link>
		<dc:creator>Tom C</dc:creator>
		<pubDate>Mon, 14 Jan 2008 16:12:07 +0000</pubDate>
		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comment-65</guid>
		<description>Looks nice, Danilo.  You might, however, want to look at Structs in Ruby.  You can replace your class definition with the following line:

LabelValue = Struct.new(:label, :value)

This will create a new class called LabelValue with accessors for label and value.  Plus, it creates a constructor, so you can create instances like this:

obj = LabelValue.new(&#039;mylabel&#039;, &#039;myvalue&#039;)

Also, you may wish to look at Ruby ranges to do the year array.  Your constant could also be created like this:

YEARS_ARRAY = (1920..2008).entries

By the way, your year_lookup re-builds the select collection array every time it is called.  You might want to put it in a constant (or class variable).  Here is how I would do it:

YEARS_SELECT_COLLECTION = YEARS_ARRAY.collect {&#124;yr&#124; LabelValue.new(yr,yr)}

Happy New Year.
-Tc</description>
		<content:encoded><![CDATA[<p>Looks nice, Danilo.  You might, however, want to look at Structs in Ruby.  You can replace your class definition with the following line:</p>
<p>LabelValue = Struct.new(:label, :value)</p>
<p>This will create a new class called LabelValue with accessors for label and value.  Plus, it creates a constructor, so you can create instances like this:</p>
<p>obj = LabelValue.new(&#8216;mylabel&#8217;, &#8216;myvalue&#8217;)</p>
<p>Also, you may wish to look at Ruby ranges to do the year array.  Your constant could also be created like this:</p>
<p>YEARS_ARRAY = (1920..2008).entries</p>
<p>By the way, your year_lookup re-builds the select collection array every time it is called.  You might want to put it in a constant (or class variable).  Here is how I would do it:</p>
<p>YEARS_SELECT_COLLECTION = YEARS_ARRAY.collect {|yr| LabelValue.new(yr,yr)}</p>
<p>Happy New Year.<br />
-Tc</p>
]]></content:encoded>
	</item>
</channel>
</rss>
