aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-16 19:20:37 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-16 13:43:53 -0800
commitbf24fe810c0591619ac01cc88d8a40423895d9d7 (patch)
tree6d952e007713e5a7147a203b1648e3acb9f36c18
parent3ef693724c4719564361b3f48f8977b795c935e0 (diff)
downloadrails-bf24fe810c0591619ac01cc88d8a40423895d9d7.tar.gz
rails-bf24fe810c0591619ac01cc88d8a40423895d9d7.tar.bz2
rails-bf24fe810c0591619ac01cc88d8a40423895d9d7.zip
belongs_to records should be initialized within the association scope
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb10
-rw-r--r--activerecord/test/cases/associations/belongs_to_associations_test.rb21
-rw-r--r--activerecord/test/models/company.rb1
3 files changed, 30 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index d311b0c572..1abea8d831 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -3,7 +3,7 @@ module ActiveRecord
module Associations
class BelongsToAssociation < AssociationProxy #:nodoc:
def create(attributes = {})
- replace(@reflection.create_association(attributes))
+ new_record(:create_association, attributes)
end
def create!(attributes = {})
@@ -11,7 +11,7 @@ module ActiveRecord
end
def build(attributes = {})
- replace(@reflection.build_association(attributes))
+ new_record(:build_association, attributes)
end
def replace(record)
@@ -34,6 +34,12 @@ module ActiveRecord
end
private
+ def new_record(method, attributes)
+ record = scoped.scoping { @reflection.send(method, attributes) }
+ replace(record)
+ record
+ end
+
def update_counters(record)
counter_cache_name = @reflection.counter_cache_column
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb
index ef6c482f67..01073bca3d 100644
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -608,4 +608,25 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal groucho, sponsor.sponsorable
assert_equal groucho, sponsor.thing
end
+
+ def test_build_with_conditions
+ client = companies(:second_client)
+ firm = client.build_bob_firm
+
+ assert_equal "Bob", firm.name
+ end
+
+ def test_create_with_conditions
+ client = companies(:second_client)
+ firm = client.create_bob_firm
+
+ assert_equal "Bob", firm.name
+ end
+
+ def test_create_bang_with_conditions
+ client = companies(:second_client)
+ firm = client.create_bob_firm!
+
+ assert_equal "Bob", firm.name
+ end
end
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index e8a126fb28..3e219fbe4a 100644
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -123,6 +123,7 @@ class Client < Company
belongs_to :firm_with_primary_key, :class_name => "Firm", :primary_key => "name", :foreign_key => "firm_name"
belongs_to :firm_with_primary_key_symbols, :class_name => "Firm", :primary_key => :name, :foreign_key => :firm_name
belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true
+ belongs_to :bob_firm, :class_name => "Firm", :foreign_key => "client_of", :conditions => { :name => "Bob" }
has_many :accounts, :through => :firm
belongs_to :account