Permalink
Browse files

Apply scoping during initialize instead of create. Fixes setting of f…

…oreign 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
  • Loading branch information...
1 parent dd6826e commit e24d2f72344baecbcd6fbb76fe3a3171762182e0 Tobias Lütke committed Jan 12, 2007
View
@@ -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]
@@ -447,7 +447,6 @@ def create(attributes = nil)
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 @@ def initialize(attributes = nil)
@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
@@ -721,7 +721,6 @@ def create!(attributes = nil)
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
@@ -750,6 +750,13 @@ def test_create_many
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

0 comments on commit e24d2f7

Please sign in to comment.