aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-12-27 14:28:19 +0530
committerPratik Naik <pratiknaik@gmail.com>2009-12-27 14:28:19 +0530
commitd511de0261003aae4913e3c24f76df6e03a35916 (patch)
tree56b9885b07709efd1df16611fa733030df5c29ea /activerecord/lib
parent8829d6ecc6b3e1a36a41decaf8237f6024be2c06 (diff)
downloadrails-d511de0261003aae4913e3c24f76df6e03a35916.tar.gz
rails-d511de0261003aae4913e3c24f76df6e03a35916.tar.bz2
rails-d511de0261003aae4913e3c24f76df6e03a35916.zip
Add find_or_create_by_* and find_or_initialize_by_* to relations
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index bcae352cc6..f17b889c53 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -142,6 +142,8 @@ module ActiveRecord
if match.finder?
find_by_attributes(match, attributes, *args)
+ elsif match.instantiator?
+ find_or_instantiator_by_attributes(match, attributes, *args, &block)
end
else
super
@@ -159,6 +161,27 @@ module ActiveRecord
end
end
+ def find_or_instantiator_by_attributes(match, attributes, *args)
+ guard_protected_attributes = false
+
+ attributes_for_create = conditions = attributes.inject({}) {|h, a| h[a] = args[attributes.index(a)]; h}
+
+ if args[0].is_a?(Hash)
+ guard_protected_attributes = true
+ conditions = args[0].with_indifferent_access.slice(*attributes).symbolize_keys
+ end
+
+ record = where(conditions).first
+
+ unless record
+ record = @klass.new { |r| r.send(:attributes=, attributes_for_create, guard_protected_attributes) }
+ yield(record) if block_given?
+ record.save if match.instantiator == :create
+ end
+
+ record
+ end
+
def create_new_relation(relation, readonly = @readonly, preload = @associations_to_preload, eager_load = @eager_load_associations)
Relation.new(@klass, relation, readonly, preload, eager_load)
end