diff options
author | Xavier Noria <fxn@hashref.com> | 2011-02-21 11:42:26 +0100 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2011-02-21 11:42:26 +0100 |
commit | f826e058355606d1b0503b0395519728335324f4 (patch) | |
tree | 4ad040efe6e5691bb408e8ebc0b6df4a216a1730 /activerecord/lib/active_record | |
parent | cd12c369961a612f6b083fc0e3b877cf59b737f4 (diff) | |
parent | b481574a33764e2db1caf01c233a9c9ac9723780 (diff) | |
download | rails-f826e058355606d1b0503b0395519728335324f4.tar.gz rails-f826e058355606d1b0503b0395519728335324f4.tar.bz2 rails-f826e058355606d1b0503b0395519728335324f4.zip |
Merge branch 'master' of git://github.com/lifo/docrails
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations.rb | 18 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations/associated.rb | 4 |
3 files changed, 27 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index cb684c1109..c6cd8891e3 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -176,6 +176,12 @@ module ActiveRecord # # # Update all books that match conditions, but limit it to 5 ordered by date # Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 + # + # # Conditions from the current relation also works + # Book.where('title LIKE ?', '%Rails%').update_all(:author => 'David') + # + # # The same idea applies to limit and order + # Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David') def update_all(updates, conditions = nil, options = {}) if conditions || options.present? where(conditions).apply_finder_options(options.slice(:limit, :order)).update_all(updates) @@ -249,6 +255,7 @@ module ActiveRecord # # Person.destroy_all("last_login < '2004-04-04'") # Person.destroy_all(:status => "inactive") + # Person.where(:age => 0..18).destroy_all def destroy_all(conditions = nil) if conditions where(conditions).destroy_all @@ -298,6 +305,7 @@ module ActiveRecord # # Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')") # Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else']) + # Post.where(:person_id => 5).where(:category => ['Something', 'Else']).delete_all # # Both calls delete the affected posts all at once with a single DELETE statement. # If you need to destroy dependent associations or call your <tt>before_*</tt> or diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 26c1a9db93..d73fce9fd0 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -1,5 +1,5 @@ module ActiveRecord - # = Active Record Validations + # = Active Record RecordInvalid # # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the # +record+ method to retrieve the record which did not validate. @@ -18,6 +18,13 @@ module ActiveRecord end end + # = Active Record Validations + # + # Active Record includes the majority of its validations from <tt>ActiveModel::Validations</tt> + # all of which accept the <tt>:on</tt> argument to define the context where the + # validations are active. Active Record will always supply either the context of + # <tt>:create</tt> or <tt>:update</tt> dependent on whether the model is a + # <tt>new_record?</tt>. module Validations extend ActiveSupport::Concern include ActiveModel::Validations @@ -49,7 +56,14 @@ module ActiveRecord perform_validations(options) ? super : raise(RecordInvalid.new(self)) end - # Runs all the specified validations and returns true if no errors were added otherwise false. + # Runs all the validations within the specified context. Returns true if no errors are found, + # false otherwise. + # + # If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if + # <tt>new_record?</tt> is true, and to <tt>:update</tt> if it is not. + # + # Validations with no <tt>:on</tt> option will run no matter the context. Validations with + # some <tt>:on</tt> option will only run in the specified context. def valid?(context = nil) context ||= (new_record? ? :create : :update) output = super(context) diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index 183acd73b8..3a783aeb00 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -33,7 +33,9 @@ module ActiveRecord # # Configuration options: # * <tt>:message</tt> - A custom error message (default is: "is invalid") - # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>). + # * <tt>:on</tt> - Specifies when this validation is active. Runs in all + # validation contexts by default (+nil+), other options are <tt>:create</tt> + # and <tt>:update</tt>. # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. |