mirror of
https://github.com/github/rails.git
synced 2026-02-10 14:15:00 -05:00
find_or_create_by_* takes a hash so you can create with more attributes than are in the method name. Closes #7368.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6420 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* find_or_create_by_* takes a hash so you can create with more attributes than are in the method name. For example, Person.find_or_create_by_name(:name => 'Henry', :comments => 'Hi new user!') is equivalent to Person.find_by_name('Henry') || Person.create(:name => 'Henry', :comments => 'Hi new user!'). #7368 [Josh Susser]
|
||||
|
||||
* Make sure with_scope takes both :select and :joins into account when setting :readonly. Allows you to save records you retrieve using method_missing on a has_many :through associations. [Koz]
|
||||
|
||||
* Allow a polymorphic :source for has_many :through associations. Closes #7143 [protocool]
|
||||
|
||||
@@ -188,6 +188,13 @@ module ActiveRecord #:nodoc:
|
||||
# winter = Tag.find_or_initialize_by_name("Winter")
|
||||
# winter.new_record? # true
|
||||
#
|
||||
# To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of
|
||||
# a list of parameters. For example:
|
||||
#
|
||||
# Tag.find_or_create_by_name(:name => "rails", :creator => current_user)
|
||||
#
|
||||
# That will either find an existing tag named "rails", or create a new one while setting the user that created it.
|
||||
#
|
||||
# == Saving arrays, hashes, and other non-mappable objects in text columns
|
||||
#
|
||||
# Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method +serialize+.
|
||||
@@ -1254,8 +1261,13 @@ module ActiveRecord #:nodoc:
|
||||
attribute_names = extract_attribute_names_from_match(match)
|
||||
super unless all_attributes_exists?(attribute_names)
|
||||
|
||||
attributes = construct_attributes_from_arguments(attribute_names, arguments)
|
||||
options = { :conditions => attributes }
|
||||
if arguments[0].is_a?(Hash)
|
||||
attributes = arguments[0].with_indifferent_access
|
||||
find_attributes = attributes.slice(*attribute_names)
|
||||
else
|
||||
find_attributes = attributes = construct_attributes_from_arguments(attribute_names, arguments)
|
||||
end
|
||||
options = { :conditions => find_attributes }
|
||||
set_readonly_option!(options)
|
||||
|
||||
find_initial(options) || send(instantiator, attributes)
|
||||
|
||||
@@ -385,6 +385,17 @@ class FinderTest < Test::Unit::TestCase
|
||||
assert !another.new_record?
|
||||
end
|
||||
|
||||
def test_find_or_create_from_one_attribute_and_hash
|
||||
number_of_companies = Company.count
|
||||
sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
||||
assert_equal number_of_companies + 1, Company.count
|
||||
assert_equal sig38, Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
||||
assert !sig38.new_record?
|
||||
assert_equal "38signals", sig38.name
|
||||
assert_equal 17, sig38.firm_id
|
||||
assert_equal 23, sig38.client_of
|
||||
end
|
||||
|
||||
def test_find_or_initialize_from_one_attribute
|
||||
sig38 = Company.find_or_initialize_by_name("38signals")
|
||||
assert_equal "38signals", sig38.name
|
||||
@@ -397,6 +408,14 @@ class FinderTest < Test::Unit::TestCase
|
||||
assert_equal "John", another.author_name
|
||||
assert another.new_record?
|
||||
end
|
||||
|
||||
def test_find_or_initialize_from_one_attribute_and_hash
|
||||
sig38 = Company.find_or_initialize_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
|
||||
assert_equal "38signals", sig38.name
|
||||
assert_equal 17, sig38.firm_id
|
||||
assert_equal 23, sig38.client_of
|
||||
assert sig38.new_record?
|
||||
end
|
||||
|
||||
def test_find_with_bad_sql
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
|
||||
|
||||
Reference in New Issue
Block a user