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

Apply scoping during initialize instead of create. Fixes setting of foreign key when using find_or_initialize_by with scoping.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5913 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Tobias Lütke 2007-01-12 21:14:36 +00:00
parent dd6826eba3
commit e24d2f7234
4 changed files with 10 additions and 2 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Apply scoping during initialize instead of create. Fixes setting of foreign key when using find_or_initialize_by with scoping. [Cody Fauser]
* SQLServer: handle [quoted] table names. #6635 [rrich]
* acts_as_nested_set works with single-table inheritance. #6030 [Josh Susser]

View file

@ -447,7 +447,6 @@ module ActiveRecord #:nodoc:
attributes.collect { |attr| create(attr) }
else
object = new(attributes)
scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create)
object.save
object
end
@ -1504,6 +1503,7 @@ module ActiveRecord #:nodoc:
@new_record = true
ensure_proper_type
self.attributes = attributes unless attributes.nil?
self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create)
yield self if block_given?
end

View file

@ -721,7 +721,6 @@ module ActiveRecord
attributes.collect { |attr| create(attr) }
else
object = new(attributes)
scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create)
object.save!
object
end

View file

@ -750,6 +750,13 @@ class HasManyAssociationsTest < Test::Unit::TestCase
companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end
def test_find_or_initialize
the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client")
assert_equal companies(:first_firm).id, the_client.firm_id
assert_equal "Yet another client", the_client.name
assert the_client.new_record?
end
def test_find_or_create
number_of_clients = companies(:first_firm).clients.size