einzige / date_time_attribute

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

Problem with dynamic attributes in Rails #2

Closed daniel-rikowski closed 10 years ago

daniel-rikowski commented 10 years ago

I think I found a new problem in the latest version.

This line fails in Rails (4.0.2)

alias_method :"old_#{attribute}=", :"#{attribute}="

I think it is because Rails creates the methods the first time method_missing is called:

def method_missing(method, *args, &block) # :nodoc:
  unless self.class.attribute_methods_generated?
    self.class.define_attribute_methods
    # More stuff removed
  else
    super
  end
end

Since date_time_attribute is called before method_missing can be called on any instance, the attribute methods don't yet exist.

But I'm not 100% sure that this is the root cause

I'm also not sure how this can be solved, but at least it is easy to reproduce, just put this in any Rails model:

include DateTimeAttribute
date_time_attribute :created_at

You'll get a NameError undefined method `created_at=' as soon as the class is loaded.

daniel-rikowski commented 10 years ago

I just checked, calling define_attribute_methods manually solves the problem, but that feels really hacky :)

define_attribute_methods
include DateTimeAttribute
date_time_attribute :created_at
einzige commented 10 years ago

Thanks Daniel! Gimme a sec, I'll fix that

RobertGauld commented 10 years ago

einzige any idea when you'll release this fix?

einzige commented 10 years ago

@robertgauld , @daniel-rikowski sorry guys, forgot about it. I will do today.

einzige commented 10 years ago

Thanks guys, bundled 0.0.6. @daniel-rikowski you were 100% right, I was able to reproduce it the way you described and fix it the way you proposed.

https://github.com/einzige/date_time_attribute/commit/923f7c2418f4e9f606197151014031bac0a3b658#diff-1e95ae95ab1149cac143cff383f33c7eR1