Skip to content

Instantly share code, notes, and snippets.

@paulsturgess
Created May 2, 2013 08:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsturgess/5500858 to your computer and use it in GitHub Desktop.
Save paulsturgess/5500858 to your computer and use it in GitHub Desktop.
How to set an attr_accessor date via Rails date_select and have Rails handle the multi-attributes automatically.

Your Class:

class YourClass < ActiveRecord::Base
  attr_accessor :some_date
  columns_hash["some_date"] = ActiveRecord::ConnectionAdapters::Column.new("some_date", nil, "date")
end

Your View:

<%= form_for :your_class do |f| %>
  <%= f.date_select :some_date %>
  <%= f.submit %>
<% end %>

Your Controller:

@your_class.attributes = params[:your_class]
@seocahill
Copy link

Thanks Paul I was searching around for ages trying to replace composed_of, great tip.

@aaron
Copy link

aaron commented May 28, 2015

I had change the last parameter to Column.new to ActiveRecord::Type::Date.new to get this working in Rails 4.2.

columns_hash["some_date"] = ActiveRecord::ConnectionAdapters::Column.new("some_date", nil, ActiveRecord::Type::Date.new)

@victor95pc
Copy link

aaron thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment