Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add create_association! for belongs_to

  • Loading branch information...
commit 52c47556b7cf55549f97f3cfd5f69b2563198eac 1 parent 8f88a28
Jon Leighton authored January 16, 2011 tenderlove committed January 16, 2011
6  activerecord/lib/active_record/associations.rb
@@ -1533,10 +1533,10 @@ def collection_accessor_methods(reflection, writer = true)
1533 1533
 
1534 1534
         def association_constructor_methods(reflection)
1535 1535
           constructors = {
1536  
-            "build_#{reflection.name}"  => "build",
1537  
-            "create_#{reflection.name}" => "create"
  1536
+            "build_#{reflection.name}"   => "build",
  1537
+            "create_#{reflection.name}"  => "create",
  1538
+            "create_#{reflection.name}!" => "create!"
1538 1539
           }
1539  
-          constructors["create_#{reflection.name}!"] = "create!" if reflection.macro == :has_one
1540 1540
 
1541 1541
           constructors.each do |name, proxy_name|
1542 1542
             redefine_method(name) do |*params|
4  activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -6,6 +6,10 @@ def create(attributes = {})
6 6
         replace(@reflection.create_association(attributes))
7 7
       end
8 8
 
  9
+      def create!(attributes = {})
  10
+        build(attributes).tap { |record| record.save! }
  11
+      end
  12
+
9 13
       def build(attributes = {})
10 14
         replace(@reflection.build_association(attributes))
11 15
       end
17  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -120,6 +120,23 @@ def test_building_the_belonging_object_with_primary_key
120 120
     assert_equal apple.name, client.firm_name
121 121
   end
122 122
 
  123
+  def test_create!
  124
+    client  = Client.create!(:name => "Jimmy")
  125
+    account = client.create_account!(:credit_limit => 10)
  126
+    assert_equal account, client.account
  127
+    assert account.persisted?
  128
+    client.save
  129
+    client.reload
  130
+    assert_equal account, client.account
  131
+  end
  132
+
  133
+  def test_failing_create!
  134
+    client  = Client.create!(:name => "Jimmy")
  135
+    assert_raise(ActiveRecord::RecordInvalid) { client.create_account! }
  136
+    assert_not_nil client.account
  137
+    assert client.account.new_record?
  138
+  end
  139
+
123 140
   def test_natural_assignment_to_nil
124 141
     client = Client.find(3)
125 142
     client.firm = nil
1  activerecord/test/models/company.rb
@@ -124,6 +124,7 @@ class Client < Company
124 124
   belongs_to :firm_with_primary_key_symbols, :class_name => "Firm", :primary_key => :name, :foreign_key => :firm_name
125 125
   belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true
126 126
   has_many :accounts, :through => :firm
  127
+  belongs_to :account
127 128
 
128 129
   # Record destruction so we can test whether firm.clients.clear has
129 130
   # is calling client.destroy, deleting from the database, or setting
1  activerecord/test/schema/schema.rb
@@ -154,6 +154,7 @@ def create_table(*args, &block)
154 154
     t.string  :name
155 155
     t.integer :client_of
156 156
     t.integer :rating, :default => 1
  157
+    t.integer :account_id
157 158
   end
158 159
 
159 160
   add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index"

0 notes on commit 52c4755

Please sign in to comment.
Something went wrong with that request. Please try again.