Skip to content

ActiveModel: adding validations dynamically fails #5449

@bjoernbur

Description

@bjoernbur

I'm experiencing a strange bug using ActiveModel::Validations. When I add validations to a specific instance using class_eval. It works on the first instance but fails on the second. Following a small setup to illustrate the problem:

require 'active_model'

class Example
  include ActiveModel::Validations

  attr_accessor :name, :email
  attr_accessor :something, :else
end

e = Example.new
e.singleton_class.validates_presence_of :name, :email

e2 = Example.new
e2.singleton_class.validates_presence_of :something, :else

e.valid? # => false
e2.valid?

this results in:

/Users/bbu0103/.rvm/gems/ruby-1.9.2-p290@cookpit/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:407:in `_run__3658204812670346400__validate__2880682970808709992__callbacks': undefined local variable or method `_callback_before_1' for #<Example:0x0000010292b7a8> (NameError)
    from /Users/bbu0103/.rvm/gems/ruby-1.9.2-p290@cookpit/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /Users/bbu0103/.rvm/gems/ruby-1.9.2-p290@cookpit/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
    from /Users/bbu0103/.rvm/gems/ruby-1.9.2-p290@cookpit/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Users/bbu0103/.rvm/gems/ruby-1.9.2-p290@cookpit/gems/activemodel-3.2.2/lib/active_model/validations.rb:212:in `run_validations!'
    from /Users/bbu0103/.rvm/gems/ruby-1.9.2-p290@cookpit/gems/activemodel-3.2.2/lib/active_model/validations.rb:179:in `valid?'
    from validation_test.rb:17:in `<main>'

I used rails 3.1.x before and this worked as expected. I guess the problem was introduced in rails 3.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions