This project is archived and is in readonly mode.
Validators bleeding from one model into another.
-
Neeraj Singh
can you list all your 11 validations. I randomly added 5 validations and I am getting the count as 5.
-
jdwyah
validates_presence_of :first_name, :last_name, :address1, :city, :state, :zip_code, :phone_number,
:if => Proc.new { |user| user.signup_step > 0 }validates_numericality_of :zip_code, :phone_number,
:if => Proc.new { |user| user.signup_step > 0 }How's this for weird:
(rdb:3) p Piece.validators.count 40
(rdb:3) p User.validators.count 40
(rdb:3) p MembershipRequest.validators.count 40
(rdb:3) p Artist.validators.count 41
(rdb:3) p MembershipRequest.validators.count 41!?
-
jdwyah
Neeraj, did you try two models, five validations each?
-
Neeraj Singh
what version of rails you are using?
-
Neeraj Singh
works fine for me
ruby-1.8.7-p249 > User.validators.count => 2 ruby-1.8.7-p249 > Comment.validators.count => 2 ActiveRecord::Schema.define(:version => 20100625031507) do create_table "comments", :force => true do |t| t.string "first_name" t.string "last_name" t.string "address1" t.string "city" t.string "state" t.string "zip_code" t.string "phone_number" end create_table "users", :force => true do |t| t.string "first_name" t.string "last_name" t.string "address1" t.string "city" t.string "state" t.string "zip_code" t.string "phone_number" end end class Comment < ActiveRecord::Base validates_presence_of :first_name, :last_name, :address1, :city, :state, :zip_code, :phone_number,:if => Proc.new { |user| user.signup_step > 0 } validates_numericality_of :zip_code, :phone_number, :if => Proc.new { |user| user.signup_step > 0 } end class User < ActiveRecord::Base validates_presence_of :first_name, :last_name, :address1, :city, :state, :zip_code, :phone_number,:if => Proc.new { |user| user.signup_step > 0 } validates_numericality_of :zip_code, :phone_number, :if => Proc.new { |user| user.signup_step > 0 } end #rails -v : Rails 3.0.0.beta4 #ruby -v : ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10.3.0] -
jdwyah
(rdb:1) p User._validators == MembershipRequest._validators trueSeems like that's the root of the issue. I see that this uses the new
class_attribute :_validatorsIt makes sense that if this class_attribute subclassing were to malfunction I would see the errors I'm getting. Obviously it works for you, but I can't pinpoint what else I may have done to break class_attribute.
I get the same results for 3.0.0.beta4 & 3.0.0.beta3
-
Neeraj Singh
- Tag changed from validations to rails3, validations
If you look at the very bottom of validators.rb in AM you will see this
# Copy validators on inheritance. def inherited(base) dup = _validators.dup base._validators = dup.each { |k, v| dup[k] = v.dup } super endIt means each model is getting its own copy of _validators. This is what I am getting
ruby-1.8.7-p249 > p User._validators == Comment._validators false => nil ruby-1.8.7-p249 > p User._validators.equal? Comment._validators false => nilI am running rails edge. YOu mentioned beta4. Can you try with edge?
-
jdwyah
Looks like that did it! Rest of my gems aren't happy with edge so can't test the actual failure, but at least the objects aren't sharing validators.
Yikes. That was a scary one.
-
Neeraj Singh
- State changed from new to invalid
Now I remember. This issue was raised earlier and I patched it http://github.com/rails/rails/commit/e11bb95d56ed77b10d54d9dfe5a3cc... . Later Mr. Valim changed it further.