Forum: Ruby on Rails Weirdness with validates_presence_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel -. (Guest)
on 2006-05-22 18:07
(Received via mailing list)
Hi,

I have a call to validates_presence_of in my model and it is behaving
very
strangely

my first stab when this happened during testing was

validates_presence_of :impact, :ease_to_implement if Proc.new { |record|
!record.cost_estimate.nil? }

I have three values in my model that I care about with this one.
Basically
what I'm trying to get to happen is
if one is there they all must be there.  (there are a couple more of
these
validates_presence_of with the orders swapped around)

but this executes the validation every time regardless of if there is
any of
these set.

>From here I tried some other things...  The most basic that I tried was

validates_presence_of :impact if Proc.new{ false }
and also
validates_presence_of :impact if Proc.new{ true }

and the validation still happened in my test.  (also in the console)

What is going on??

PS
Windoze XP
ruby 1.8.4
rails 1.1.2
Daniel -. (Guest)
on 2006-05-22 18:35
(Received via mailing list)
Sorry to post a reply to my own post.

A bit simple really.

the if should be an argument to the method ie. v_p_o :impact, :if =>
Proc.new{ some thing } instead of v_p_o :impact if ...
Bill W. (Guest)
on 2006-05-22 19:43
(Received via mailing list)
Hi Daniel,

Daniel N wrote:

> Sorry to post a reply to my own post.

No need to apologize for what is, in fact, the preferred behavior.  If
everyone did this it would be a lot easier for folks to use the archives
to
find answers to their problems which, in turn, would increase bandwidth
on
the list to address new problems.  Good on you, bud.

Best regards,
Bill
This topic is locked and can not be replied to.