Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

_validators works on ActiveRecord::Base instead of individual models

#5341

When I have

# app/model/ingredients.rb
class Ingredient < ActiveRecord::Base
  attr_accessible :product, :name, :quantity
  belongs_to :product
  validates :product_id, :presence => true
end

# app/model/products.rb
class Product < ActiveRecord::Base
  attr_accessible :name
  has_many :ingredients
  validates :name, :presence => true
  accepts_nested_attributes_for :ingredients, :allow_destroy => true
end

And in Rails console I do this

$ rails c
Loading development environment (Rails 3.0.0.beta4)
ruby-1.9.2-rc2 > Ingredient.validators_on(:name)
 => [] 
ruby-1.9.2-rc2 > p = Product.new
 => #<Product id: nil, name: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-rc2 > p.class == Product
 => true 
ruby-1.9.2-rc2 > p.ingredients.build
 => #<Ingredient id: nil, product_id: nil, name: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-rc2 > p.ingredients[0]   
 => #<Ingredient id: nil, product_id: nil, name: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-rc2 > p.ingredients[0].class
 => Ingredient(id: integer, product_id: integer, name: string, created_at: datetime, updated_at: datetime) 
ruby-1.9.2-rc2 > p.ingredients[0].class == Ingredient
 => true 
ruby-1.9.2-rc2 > p.ingredients[0].class.validators_on(:name)
 => [#<ActiveModel::Validations::PresenceValidator:0x00000002831920 @attributes=[:name], @options={}>]
ruby-1.9.2-rc2 > Ingredient.validators_on(:name)
 => [#<ActiveModel::Validations::PresenceValidator:0x00000002831920 @attributes=[:name], @options={}>]
ruby-1.9.2-rc2 >

I expect the Ingredient.validators_on(:name) to be [] as in the beginning.

Reported by TMaYaD · August 9th, 2010 @ 07:43 PM

State: resolved
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. TMaYaD
    TMaYaD

    Something really funny is going on here.

    $ rails c
    Loading development environment (Rails 3.0.0.beta4)
    ruby-1.9.2-rc2 > Ingredient.validators_on(:name)
     => [] 
    ruby-1.9.2-rc2 > Product.validators_on(:name)
     => [#<ActiveModel::Validations::PresenceValidator:0x00000002094118 @attributes=[:name], @options={}>] 
    ruby-1.9.2-rc2 > Ingredient.validators_on(:name)
     => [#<ActiveModel::Validations::PresenceValidator:0x00000002094118 @attributes=[:name], @options={}>]
    

    August 10th, 2010 @ 01:54 AM

  2. TMaYaD
    TMaYaD
    • Assigned user set to José Valim
    • Tag changed from nested_attributes_for, reflection, validation, validations to reflection, validation, validations

    Probing further it seems this behavior is universal and not limited to nested attributes
    The validations keep piling up to the previously queried ones

    In the diff for the commit that added this feature ( http://github.com/rails/rails/commit/8f97e9d19abf02b33c5f7c0c1f1d5d... ) self refers to ActiveResource::Base or ActiveRecord::Base but not the model itself as expected.

    Assigning to Jose Valim as he is likely to have more insight into this.

    August 10th, 2010 @ 03:02 AM

  3. TMaYaD
    TMaYaD
    • Title changed from Validators_on an attribute of nested model returns validatations on attribute with same name from parent model to _validators works on ActiveRecord::Base instead of individual models

    August 10th, 2010 @ 05:38 AM

  4. TMaYaD
  5. José Valim
    José Valim
    • State changed from new to resolved
    • Importance changed from to Low

    I believe this is fixed in RC.

    August 12th, 2010 @ 05:03 PM