Validates_with problem

11 views
Skip to first unread message

Misha Ognev

unread,
Oct 14, 2011, 2:53:20 AM10/14/11
to rubyonra...@googlegroups.com
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?

--
Posted via http://www.ruby-forum.com/.

Tim Shaffer

unread,
Oct 14, 2011, 7:59:58 AM10/14/11
to rubyonra...@googlegroups.com
On Friday, October 14, 2011 2:53:20 AM UTC-4, Ruby-Forum.com User wrote:
I have one big problem: when I try record to db through
update_attributes! (which must save when record invalid).

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


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

Misha Ognev

unread,
Oct 15, 2011, 2:45:27 AM10/15/11
to rubyonra...@googlegroups.com
Tim, I know that somethn going wrong. But don't know, what.

Colin Law

unread,
Oct 15, 2011, 3:56:24 AM10/15/11
to rubyonra...@googlegroups.com
On 15 October 2011 07:45, Misha Ognev <li...@ruby-forum.com> wrote:
> 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

Peter Vandenabeele

unread,
Oct 16, 2011, 11:46:57 AM10/16/11
to rubyonra...@googlegroups.com
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:

<code>
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
Reply all
Reply to author
Forward
0 new messages