Forum: Ruby on Rails where is save_without_validation ?

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.
3f38541776a2513a20ea2f1c2d522cb3?d=identicon&s=25 Perry Smith (pedz)
on 2007-07-08 07:31
Can someone tell me where this is at or how it gets created?  There must
be some magic to it.

Thank you,
Perry
Df4978da52459529e539da4c995918cb?d=identicon&s=25 Zach Inglis // LT3media (Guest)
on 2007-07-08 10:45
(Received via mailing list)
>> a = Author.new(:name => "Test")
=> #<Author:0x34eaa54 @attributes={"name"=>"Test", "email"=>nil,
"created_at"=>nil}, @new_record=true>
 >> a.save
=> false
 >> a.errors
=> #<ActiveRecord::Errors:0x34e92f8 @base=#<Author:0x34eaa54
@errors=#<ActiveRecord::Errors:0x34e92f8 ...>, @attributes=
{"name"=>"Test", "modified_at"=>nil, "url"=>nil, "is_active"=>false,
"hashed_pass"=>nil, "email"=>nil, "created_at"=>nil},
@new_record=true>, @errors={"email"=>["can't be blank"]}>
 >> a.save(false)
=> true

Zach Inglis
→ Blog -- http://www.zachinglis.com
→ Company -- http://www.lt3media.com
→ Portfolio -- http://portfolio.zachinglis.com
→ Web Designer → Print Designer → Identity Desginer → Ruby on
Rails developer → et plus.
→ AIM: zachinglis  → MSN: zachinglis@hotmail.com  → Others
available on request
3f38541776a2513a20ea2f1c2d522cb3?d=identicon&s=25 Perry Smith (pedz)
on 2007-07-08 14:47
Perry Smith wrote:
> Can someone tell me where this is at or how it gets created?  There must
> be some magic to it.
>
In active_support/core_ext/module/aliasing.rb, there is a instance
method added to module called alias_method_chain.  It is used in a lot
of places, like validations.rb around line 221.  You see:

        alias_method_chain :save, :validation
        alias_method_chain :save!, :validation
        alias_method_chain :update_attribute, :validation_skipping

This creates two aliases as described by the comments:

  #   alias_method_chain :foo?, :feature
  #
  # is equivalent to
  #
  #   alias_method :foo_without_feature?, :foo?
  #   alias_method :foo?, :foo_with_feature?

So, in this case save_without_validation points to what save use to
point to and now save points to save_with_validation.  So, in the code,
you will see a call to save and in the stack trace.  But the line
numbers will be inside save_with_validation.  (or foo_with_feature in
the general case).   foo_with_feature will then usually call
foo_without_feature to call the original foo routine.

Pretty nifty trick.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-07-08 16:28
(Received via mailing list)
Zach Inglis // LT3media wrote:

> >> a.save(false)
> => true

A> What's wrong with calling save_without_validation? Is that
      /de-facto/ private?

B> validations involving unique table keys require lots of
       database hits, so saving without validation is faster,
       right?

--
  Phlip
  http://www.oreilly.com/catalog/9780596510657/
  "Test Driven Ajax (on Rails)"
  assert_xpath, assert_javascript, & assert_ajax
This topic is locked and can not be replied to.