diff options
author | Luciano G Panaro <contact@decodeuri.com> | 2009-09-26 10:33:50 -0300 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2009-09-28 14:50:33 +1300 |
commit | 4168f876238982d0d584006f50188071928a8b7f (patch) | |
tree | 93e22b323a8f3bf25c5e67697a347e1fb958b0a0 /activerecord/lib/active_record/associations | |
parent | d48ebeade2d907573e3fb086495b57b10115066c (diff) | |
download | rails-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/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_one_association.rb | 9 |
1 files changed, 9 insertions, 0 deletions
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 |