date_time_attribute
date_time_attribute copied to clipboard
Assignment of multiparameter attributes
Newish to Rails, followed the docs but getting the following error:
4 error(s) on assignment of multiparameter attributes [error on assignment [11, 8, 2014] to start_date (undefined method `klass' for nil:NilClass),error on assignment [12, 8, 2014] to end_date (undefined method `klass' for nil:NilClass),error on assignment [2014, 8, 11, 7, 45] to start_time (undefined method `klass' for nil:NilClass),error on assignment [2014, 8, 11, 8, 45] to end_time (undefined method `klass' for nil:NilClass)]
Code - https://gist.github.com/habitullence/533730edef77f4e1a48a
Please can you share the code you're using which generates those errors.
Hi @robertgauld, I've created a gist and updated the description.
Can you humor me and make the following chnage, it shouldn't make a difference but it just might.
# app/models/event.rb
class Event < ActiveRecord::Base
date_time_attribute :start
date_time_attribute :end
end
@robertgauld we can easily start from specs here :)
@robertgauld negatory, same error.
+1 getting same error here. Also using separate date and time selects.
fyi: http://arjanvandergaag.nl/blog/rails_attributes_magic.html gives a good run down on whats going on, rails is just assigning these virtual attributes as strings.
@habitullence There's a way around this:
in your case:
# app/models/event.rb
class Event < ActiveRecord::Base
date_time_attribute :start, :end
self.columns_hash['start_time'] = OpenStruct.new(type: :time, klass: Time)
self.columns_hash['start_date'] = OpenStruct.new(type: :date, klass: Date)
self.columns_hash['end_time'] = OpenStruct.new(type: :time, klass: Time)
self.columns_hash['end_date'] = OpenStruct.new(type: :date, klass: Date)
end
You can alternatively swap out the OpenStruct
for something like : ActiveRecord::ConnectionAdapters::Column.new("expiration_date", nil, "date")
Be sure to not have ignore_date
as part of your options on your time select, or you will run into the same issue.