Ruby/Rails: converting a Date to a UNIX timestamp

The code date.to_time.to_i should work fine. The Rails console session below shows an example: >> Date.new(2009,11,26).to_time => Thu Nov 26 00:00:00 -0800 2009 >> Date.new(2009,11,26).to_time.to_i => 1259222400 >> Time.at(1259222400) => Thu Nov 26 00:00:00 -0800 2009 Note that the intermediate DateTime object is in local time, so the timestamp might be several hours off from … Read more

Rails.env vs RAILS_ENV

According to the docs, #Rails.env wraps RAILS_ENV: # File vendor/rails/railties/lib/initializer.rb, line 55 def env @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV) end But, look at specifically how it’s wrapped, using ActiveSupport::StringInquirer: Wrapping a string in this class gives you a prettier way to test for equality. The value returned by Rails.env is wrapped in a StringInquirer object so instead … Read more

Ruby on Rails form_for select field with class

Try this way: <%= f.select(:object_field, [‘Item 1’, …], {}, { :class => ‘my_style_class’ }) %> select helper takes two options hashes, one for select, and the second for html options. So all you need is to give default empty options as first param after list of items and then add your class to html_options. http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select