Docs are wrong, `:save` is not the default, the default is that the context
will be set (to either :create or :update) based on `new_record?` - :save is
never a valid value for `:on` unless you're going to pass that context to
your valid? and save calls, eg. this will both work correctly with `:on =>
:save` in your validation(s):

valid? :save  # => false
save :context => :save  # => false


But obviously that's not really what the docs were trying to suggest :)

On Wed, Feb 16, 2011 at 12:49 PM, Peer Allan <[email protected]> wrote:

> Hello all,
>
> I think I have found an interesting bug in the validations.  The
> reason I say might is because I don't know if it was intentionally
> constructed this way or not.
>
> ActiveRecord docs describe the validates_as_* methods as accepting
> the :on argument with the options of :create, :update and :save
> (default :save).  If you explicitly set argument to :on => :save that
> validation if forever skipped.  The context or state of the model
> (:create or :update) does not matter.
>
> class Topic < ActiveRecord::Base
>  validates_presence_of :title, :on => :save
> end
>
> t = Topic.new(:title => '')
> t.valid?  => true
>
> t = Topic.find(1)
> t.title = ''
> t.valid?  => true
>
> I have verified this with tests in ActiveRecord.
>
> My question is should this be fixed so that :save is a valid option or
> should it be left as is and the documentation updated to no longer
> show :save as a valid option?
>
> Related to this, if you attempt to send the :on argument to a
> validates_* of a class that uses ActiveModel::Validations#valid?
> (ActiveRecord overrides it) to do its validation then they will not
> work.
>
> class Foo
>  include ActiveModel::Validations
>
>  attr_accessor :title
>
>  validates_presence_of :title, :on => :save
> end
>
> f = Foo.new
> f.title = ''
> f.valid? => true
>
> Again, is this expected behaiour?
>
> Peer
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-core?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to