aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorLuciano G Panaro <contact@decodeuri.com>2009-09-26 10:33:50 -0300
committerMichael Koziarski <michael@koziarski.com>2009-09-28 14:50:33 +1300
commit4168f876238982d0d584006f50188071928a8b7f (patch)
tree93e22b323a8f3bf25c5e67697a347e1fb958b0a0 /activerecord/lib/active_record
parentd48ebeade2d907573e3fb086495b57b10115066c (diff)
downloadrails-4168f876238982d0d584006f50188071928a8b7f.tar.gz
rails-4168f876238982d0d584006f50188071928a8b7f.tar.bz2
rails-4168f876238982d0d584006f50188071928a8b7f.zip
Make has_one with :conditions hash scope build or creation of the associated object with those conditions
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#3088 state:committed]
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb9
2 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 266a52d612..497115e4ff 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -938,7 +938,9 @@ module ActiveRecord
# if the real class name is Person, you'll have to specify it with this option.
# [:conditions]
# Specify the conditions that the associated object must meet in order to be included as a +WHERE+
- # SQL fragment, such as <tt>rank = 5</tt>.
+ # SQL fragment, such as <tt>rank = 5</tt>. Record creation from the association is scoped if a hash
+ # is used. <tt>has_one :account, :conditions => {:enabled => true}</tt> will create an enabled account with <tt>@company.create_account</tt>
+ # or <tt>@company.build_account</tt>.
# [:order]
# Specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment,
# such as <tt>last_name, first_name DESC</tt>.
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index c2568d0c0c..b85a40b2e5 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -8,18 +8,21 @@ module ActiveRecord
def create(attrs = {}, replace_existing = true)
new_record(replace_existing) do |reflection|
+ attrs = merge_with_conditions(attrs)
reflection.create_association(attrs)
end
end
def create!(attrs = {}, replace_existing = true)
new_record(replace_existing) do |reflection|
+ attrs = merge_with_conditions(attrs)
reflection.create_association!(attrs)
end
end
def build(attrs = {}, replace_existing = true)
new_record(replace_existing) do |reflection|
+ attrs = merge_with_conditions(attrs)
reflection.build_association(attrs)
end
end
@@ -128,6 +131,12 @@ module ActiveRecord
inverse = @reflection.inverse_of
return !inverse.nil?
end
+
+ def merge_with_conditions(attrs={})
+ attrs ||= {}
+ attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
+ attrs
+ end
end
end
end