Where is save_without_validation?


#1

Can someone tell me where this is at or how it gets created? There must
be some magic to it.

Thank you,
Perry


#2

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 I.
→ 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: removed_email_address@domain.invalid → Others
available on request


#3

Perry S. 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.


#4

Zach I. // 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