1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Pushing a record on an association collection doesn't unnecessarily load all the associated records.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5769 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-12-21 23:28:12 +00:00
parent 8cb6cb58a2
commit 4cf8bf7312
4 changed files with 15 additions and 10 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Pushing a record on an association collection doesn't unnecessarily load all the associated records. [Obie Fernandez, Jeremy Kemper]
* Oracle: fix connection reset failure. #6846 [leonlleslie]
* Subclass instantiation doesn't try to explicitly require the corresponding subclass. #6840 [leei, Jeremy Kemper]

View file

@ -17,7 +17,6 @@ module ActiveRecord
# Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically.
def <<(*records)
result = true
load_target
@owner.transaction do
flatten_deeper(records).each do |record|
@ -103,7 +102,7 @@ module ActiveRecord
# calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero
# and you need to fetch that collection afterwards, it'll take one less SELECT query if you use length.
def size
if loaded? && !@reflection.options[:uniq]
if @owner.new_record? || (loaded? && !@reflection.options[:uniq])
@target.size
elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array)
unsaved_records = Array(@target.detect { |r| r.new_record? })

View file

@ -637,12 +637,15 @@ class HasManyAssociationsTest < Test::Unit::TestCase
def test_adding_before_save
no_of_firms = Firm.count
no_of_clients = Client.count
new_firm = Firm.new("name" => "A New Firm, Inc")
c = Client.new("name" => "Apple")
new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
new_firm.clients_of_firm << (c = Client.new("name" => "Apple"))
assert new_firm.new_record?
assert c.new_record?
assert_equal 1, new_firm.clients_of_firm.size
new_firm.clients_of_firm << c
assert_equal 2, new_firm.clients_of_firm.size
assert_equal no_of_firms, Firm.count # Firm was not saved to database.
assert_equal no_of_clients, Client.count # Clients were not saved to database.
assert new_firm.save
@ -651,6 +654,7 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal new_firm, c.firm
assert_equal no_of_firms+1, Firm.count # Firm was saved to database.
assert_equal no_of_clients+2, Client.count # Clients were saved to database.
assert_equal 2, new_firm.clients_of_firm.size
assert_equal 2, new_firm.clients_of_firm(true).size
end

View file

@ -1290,10 +1290,10 @@ class BasicsTest < Test::Unit::TestCase
clients = [ client_stored, client_new ]
firm.clients << clients
assert_equal clients.map(&:name).to_set, firm.clients.map(&:name).to_set
firm.clear_association_cache
assert_equal firm.clients.collect{ |x| x.name }.sort, clients.collect{ |x| x.name }.sort
assert_equal clients.map(&:name).to_set, firm.clients.map(&:name).to_set
end
def test_interpolate_sql