Validates_with problem

I have one big problem: when I try record to db through update_attributes! (which must save when record invalid). All good, all validates :field, :presence => true et.c. field ignored, but validates_with no ignore! So, it raises an error:

Validation failed: Captcha wrong captcha input

CaptchaValidator works with many models, so I put it to captcha_validator.rb

But I want to save with ignore this field. What I must do?

I’m not so sure that’s entirely accurate. Check out the API for update_attributes! and save! methods:

http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attributes-21

http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-save-21

Specifically: update_attributes! updates its receiver just like update_attributes but calls save! With save! validations always run.

Tim, I know that somethn going wrong. But don't know, what.

I think you need to explain your problem more carefully. I do not think anyone knows exactly what you are asking.

Colin

I think the OP is trying to save to the database without validaton and is

confusing

  • update_attributes (which does perform the validations)

  • update_attribute (which can update an attribute in the db without validations)

This example might help:

class Referral < ActiveRecord::Base

belongs_to :user

validates :comment, :presence => true

end

$ rails c

Loading development environment (Rails 3.1.1.rc1)

001:0> r = Referral.first

Referral Load (0.6ms) SELECT “referrals”.* FROM “referrals” LIMIT 1

=> #<Referral id: 3, user_id: 2, comment: “developer”, created_at: “2011-10-11 10:56:14”, updated_at: “2011-10-11 10:56:14”>

002:0> r.update_attributes(:comment => nil)

(0.4ms) BEGIN

(0.3ms) ROLLBACK

=> false # FAILED (because of validation)

003:0> r.errors.messages

=> {:comment=>[“can’t be blank”]} # this is the validation that fails

004:0> r.update_attribute(:comment, nil)

(0.4ms) BEGIN

(1.0ms) UPDATE “referrals” SET “comment” = NULL, “updated_at” = ‘2011-10-16 15:43:22.544048’ WHERE “referrals”.“id” = 3

(1.3ms) COMMIT

=> true # the save succeeded, without checking the validation

005:0> r

=> #<Referral id: 3, user_id: 2, comment: nil, created_at: “2011-10-11 10:56:14”, updated_at: “2011-10-16 15:43:22”>

HTH,

Peter