This project is archived and is in readonly mode.

#5341 ✓resolved
TMaYaD

_validators works on ActiveRecord::Base instead of individual models

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

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.

Comments and changes to this ticket

  • TMaYaD

    TMaYaD August 10th, 2010 @ 01:54 AM

    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={}>]
    
  • TMaYaD

    TMaYaD August 10th, 2010 @ 03:02 AM

    • 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.

  • TMaYaD

    TMaYaD August 10th, 2010 @ 05:38 AM

    • 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”
  • TMaYaD
  • José Valim

    José Valim August 12th, 2010 @ 05:03 PM

    • State changed from “new” to “resolved”
    • Importance changed from “” to “Low”

    I believe this is fixed in RC.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages