<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -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 &lt;tt&gt;rank = 5&lt;/tt&gt;.
+      #   SQL fragment, such as &lt;tt&gt;rank = 5&lt;/tt&gt;. Record creation from the association is scoped if a hash
+      #   is used. &lt;tt&gt;has_one :account, :conditions =&gt; {:enabled =&gt; true}&lt;/tt&gt; will create an enabled account with &lt;tt&gt;@company.create_account&lt;/tt&gt;
+      #   or &lt;tt&gt;@company.build_account&lt;/tt&gt;.
       # [:order]
       #   Specify the order in which the associated objects are returned as an &lt;tt&gt;ORDER BY&lt;/tt&gt; SQL fragment,
       #   such as &lt;tt&gt;last_name, first_name DESC&lt;/tt&gt;.</diff>
      <filename>activerecord/lib/active_record/associations.rb</filename>
    </modified>
    <modified>
      <diff>@@ -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</diff>
      <filename>activerecord/lib/active_record/associations/has_one_association.rb</filename>
    </modified>
    <modified>
      <diff>@@ -315,4 +315,22 @@ class HasOneAssociationsTest &lt; ActiveRecord::TestCase
       Firm.find(@firm.id, :include =&gt; :account).save!
     end
   end
+
+  def test_build_respects_hash_condition
+    account = companies(:first_firm).build_account_limit_500_with_hash_conditions
+    assert account.save
+    assert_equal 500, account.credit_limit
+  end
+
+  def test_create_respects_hash_condition
+    account = companies(:first_firm).create_account_limit_500_with_hash_conditions
+    assert       !account.new_record?
+    assert_equal 500, account.credit_limit
+  end
+
+  def test_create!_respects_hash_condition
+    account = companies(:first_firm).create_account_limit_500_with_hash_conditions!
+    assert       !account.new_record?
+    assert_equal 500, account.credit_limit
+  end
 end</diff>
      <filename>activerecord/test/cases/associations/has_one_associations_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -176,9 +176,9 @@ class ReflectionTest &lt; ActiveRecord::TestCase
 
   def test_reflection_of_all_associations
     # FIXME these assertions bust a lot
-    assert_equal 35, Firm.reflect_on_all_associations.size
+    assert_equal 36, Firm.reflect_on_all_associations.size
     assert_equal 26, Firm.reflect_on_all_associations(:has_many).size
-    assert_equal 9, Firm.reflect_on_all_associations(:has_one).size
+    assert_equal 10, Firm.reflect_on_all_associations(:has_one).size
     assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
   end
 </diff>
      <filename>activerecord/test/cases/reflection_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -81,6 +81,8 @@ class Firm &lt; Company
   has_one :account_using_foreign_and_primary_keys, :foreign_key =&gt; &quot;firm_name&quot;, :primary_key =&gt; &quot;name&quot;, :class_name =&gt; &quot;Account&quot;
   has_one :deletable_account, :foreign_key =&gt; &quot;firm_id&quot;, :class_name =&gt; &quot;Account&quot;, :dependent =&gt; :delete
   
+  has_one :account_limit_500_with_hash_conditions, :foreign_key =&gt; &quot;firm_id&quot;, :class_name =&gt; &quot;Account&quot;, :conditions =&gt; { :credit_limit =&gt; 500 }
+
   has_one :unautosaved_account, :foreign_key =&gt; &quot;firm_id&quot;, :class_name =&gt; 'Account', :autosave =&gt; false
   has_many :accounts
   has_many :unautosaved_accounts, :foreign_key =&gt; &quot;firm_id&quot;, :class_name =&gt; 'Account', :autosave =&gt; false</diff>
      <filename>activerecord/test/models/company.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>d48ebeade2d907573e3fb086495b57b10115066c</id>
    </parent>
  </parents>
  <author>
    <name>Luciano G Panaro</name>
    <login>lucianopanaro</login>
    <email>contact@decodeuri.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/4168f876238982d0d584006f50188071928a8b7f</url>
  <id>4168f876238982d0d584006f50188071928a8b7f</id>
  <committed-date>2009-09-27T18:50:33-07:00</committed-date>
  <authored-date>2009-09-26T06:33:50-07:00</authored-date>
  <message>Make has_one with :conditions hash scope build or creation of the associated object with those conditions

Signed-off-by: Michael Koziarski &lt;michael@koziarski.com&gt;
[#3088 state:committed]</message>
  <tree>93e22b323a8f3bf25c5e67697a347e1fb958b0a0</tree>
  <committer>
    <name>Michael Koziarski</name>
    <login>NZKoz</login>
    <email>michael@koziarski.com</email>
  </committer>
</commit>
