Take the 2-minute tour ×

Here is the code in my Model

 include Mongoid::Document
 include Mongoid::Timestamps

 field :message, :type => String
 field :send_at, :type => DateTime

Here is the code for my form partial

 <%= f.label :send_at %><br />
 <%= f.datetime_select :send_at %>

But the date and time is never populated. I made sure that Mongo and Mongoid are up to date as well. Not sure if there's something I'm missing.


Started POST "/notifis" for at Mon Oct 18 05:48:05 -0400 2010
Processing by NotifisController#create as HTML
Parameters: {"commit"=>"Create Notifi",
"authenticity_token"=>"/hrlnvA2Xn5NqGgCkPFAQV254IHPJEvZoLxOYNNUwhc=", "_snowman"=>"☃",
"notifi"=>{"send_at(2i)"=>"10", "is_sent"=>"0", "send_at(3i)"=>"18",
"send_at(4i)"=>"09",     "message"=>"erwer", "send_at(5i)"=>"48", 
MONGODB noti_development['notifis'].insert([{"send_at(2i)"=>"10", "created_at"=>Mon Oct 
18 09:48:05 UTC 2010, "is_sent"=>false, "updated_at"=>Mon Oct 18 09:48:05 UTC 2010, 
"_id"=>BSON::ObjectID('4cbc17d5c24d7602bc00002d'), "send_at(3i)"=>"18", 
"message"=>"Sample Message", "send_at(4i)"=>"09", "send_at(1i)"=>"2010", 
Redirected to http://localhost:3000/notifis
Completed 302 Found in 4ms

Started GET "/notifis" for at Mon Oct 18 05:48:05 -0400 2010
Processing by NotifisController#index as HTML
MONGODB noti_development['notifis'].find({}, {})
Rendered notifis/index.html.erb within layouts/application (42.0ms)
Completed 200 OK in 52ms (Views: 51.2ms)
share|improve this question
Please add some log lines so that we can see if the values are correctly supplied to the controller. –  Simone Carletti Oct 18 '10 at 10:09

2 Answers 2

up vote 8 down vote accepted

Mongoid does not handle multiparameter attributes like Date yet, so you need to do the following:

# copied from: https://gist.github.com/315227
# add this to a new file in your lib directory
module MultiParameterAttributes
  def filter_time(attributes, name)
     attrs = attributes.collect do |key, value|
       if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
         [$1.to_i, value.send("to_#$2")]
     Time.zone.local(*attrs) unless attrs.empty?

# include the module above in your application_controller.rb
class ApplicationController < ActionController::Base
  include MultiParameterAttributes

# and in the controller action where you process the form params, use filter_time
class YourController < ApplicationController
  def your_action
    time = filter_time(params, :my_time_attribute_name)

Some more info here: http://groups.google.com/group/mongoid/browse_thread/thread/f83cbdd641581912

share|improve this answer

Recent versions of Mongoid do handle multi-parameter attributes, you just have to include the module in your model:

include Mongoid::MultiParameterAttributes

Docs: http://mongoid.org/en/mongoid/docs/rails.html

share|improve this answer
Thanks Gabe. That was exactly what I needed –  Leo Moore Dec 29 '11 at 2:02
This also helped with datetime from simple_form –  Taras Kalapun Aug 24 '12 at 14:54

Your Answer


