diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-12-27 14:46:38 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-12-27 14:46:38 +0530 |
commit | 85770ec7139fcba985310d239d4c57cfe6f6c60b (patch) | |
tree | 6ac06f74e46385f86ef51feb55e69730f1ca1020 /activerecord/lib/active_record/base.rb | |
parent | d511de0261003aae4913e3c24f76df6e03a35916 (diff) | |
download | rails-85770ec7139fcba985310d239d4c57cfe6f6c60b.tar.gz rails-85770ec7139fcba985310d239d4c57cfe6f6c60b.tar.bz2 rails-85770ec7139fcba985310d239d4c57cfe6f6c60b.zip |
Make Model.find_or_create_by_* and find_or_initialize_by_* use relations and remove method caching
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 55 |
1 files changed, 1 insertions, 54 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 31737043eb..cba1e0ebe6 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1864,60 +1864,7 @@ module ActiveRecord #:nodoc: relation = options.any? ? construct_finder_arel_with_includes(options) : scoped relation.send :find_by_attributes, match, attribute_names, *arguments elsif match.instantiator? - instantiator = match.instantiator - # def self.find_or_create_by_user_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(*[:user_id]) - # else - # find_attributes = attributes = construct_attributes_from_arguments([:user_id], args) - # end - # - # options = { :conditions => find_attributes } - # set_readonly_option!(options) - # - # record = find(:first, options) - # - # if record.nil? - # record = self.new { |r| r.send(:attributes=, attributes, guard_protected_attributes) } - # yield(record) if block_given? - # record.save - # record - # else - # record - # end - # end - 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 - find_attributes = attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args) - end - - options = { :conditions => find_attributes } - set_readonly_option!(options) - - record = find(:first, options) - - 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 - record - end - end - }, __FILE__, __LINE__ - send(method_id, *arguments, &block) + scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block end elsif match = DynamicScopeMatch.match(method_id) attribute_names = match.attribute_names |