einzige / date_time_attribute

Allows to assign date, time and time zone attributes separately for a DateTime attribute.
44 stars 14 forks source link

Assignment of multiparameter attributes #15

Open nomasprime opened 10 years ago

nomasprime commented 10 years ago

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

RobertGauld commented 10 years ago

Please can you share the code you're using which generates those errors.

nomasprime commented 10 years ago

Hi @robertgauld, I've created a gist and updated the description.

RobertGauld commented 10 years ago

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
einzige commented 10 years ago

@robertgauld we can easily start from specs here :)

nomasprime commented 10 years ago

@robertgauld negatory, same error.

ridget commented 10 years ago

+1 getting same error here. Also using separate date and time selects.

ridget commented 10 years ago

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.