diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2008-03-25 23:56:48 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-03-25 23:56:48 +0000 |
commit | c10b2255b580de9b763bab28872ebf3434a16d8f (patch) | |
tree | 71162ac953b149da8e071e3f145879dda8074ce1 /activerecord/lib | |
parent | 4942e5b1cbad6ee8e9340cfd5e1f643d850eeaa0 (diff) | |
download | rails-c10b2255b580de9b763bab28872ebf3434a16d8f.tar.gz rails-c10b2255b580de9b763bab28872ebf3434a16d8f.tar.bz2 rails-c10b2255b580de9b763bab28872ebf3434a16d8f.zip |
Fixed that ActiveRecord#Base.find_or_create/initialize would not honor attr_protected/accessible when used with a hash (closes #11422) [miloops]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9090 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 4f9fd71ffc..eeb728f60d 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -255,7 +255,7 @@ module ActiveRecord #:nodoc: # actually Person.find_by_user_name(user_name, options). So you could call <tt>Payment.find_all_by_amount(50, :order => "created_on")</tt>. # # The same dynamic finder style can be used to create the object if it doesn't already exist. This dynamic finder is called with - # <tt>find_or_create_by_</tt> and will return the object if it already exists and otherwise creates it, then returns it. Example: + # <tt>find_or_create_by_</tt> and will return the object if it already exists and otherwise creates it, then returns it. Protected attributes won't be setted unless they are given in a block. For example: # # # No 'Summer' tag exists # Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer") @@ -263,7 +263,10 @@ module ActiveRecord #:nodoc: # # Now the 'Summer' tag does exist # Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer") # - # Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without saving it first. Example: + # # Now 'Bob' exist and is an 'admin' + # User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true } + # + # Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without saving it first. Protected attributes won't be setted unless they are given in a block. For example: # # # No 'Winter' tag exists # winter = Tag.find_or_initialize_by_name("Winter") @@ -1591,7 +1594,10 @@ module ActiveRecord #:nodoc: self.class_eval %{ def self.#{method_id}(*args) + guard_protected_attributes = false + if args[0].is_a?(Hash) + guard_protected_attributes = true attributes = args[0].with_indifferent_access find_attributes = attributes.slice(*[:#{attribute_names.join(',:')}]) else @@ -1602,8 +1608,10 @@ module ActiveRecord #:nodoc: set_readonly_option!(options) record = find_initial(options) - if record.nil? - record = self.new { |r| r.send(:attributes=, attributes, false) } + + if record.nil? + record = self.new { |r| r.send(:attributes=, attributes, guard_protected_attributes) } + #{'yield(record) if block_given?'} #{'record.save' if instantiator == :create} record else |