aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-09-30 22:27:02 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-09-30 22:27:02 +0100
commitdd2779e1b83b4d867d47dd286ec0c919f5df12a9 (patch)
tree6e52ea0a329c24429f4d1d41b065e082f0ed6baa /activerecord/lib
parent329b14aa8fdd291a00d17ba12c2e0ab4c3a157cc (diff)
parent420004e030e96f2ace6e27fd622c90ee9e986677 (diff)
downloadrails-dd2779e1b83b4d867d47dd286ec0c919f5df12a9.tar.gz
rails-dd2779e1b83b4d867d47dd286ec0c919f5df12a9.tar.bz2
rails-dd2779e1b83b4d867d47dd286ec0c919f5df12a9.zip
Merge commit 'mainstream/master'
Diffstat (limited to 'activerecord/lib')
-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