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

Allow find on a has_many association defined with :finder_sql to accept id arguments as strings like regular find does. Closes #9916 [krishna]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8030 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Marcel Molina 2007-10-26 03:42:28 +00:00
parent 7f8183e3c4
commit bf658a906b
5 changed files with 22 additions and 3 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Allow find on a has_many association defined with :finder_sql to accept id arguments as strings like regular find does. Closes #9916 [krishna]
* Use VALID_FIND_OPTIONS when resolving :find scoping rather than hard coding the list of valid find options. Closes #9443 [sur] * Use VALID_FIND_OPTIONS when resolving :find scoping rather than hard coding the list of valid find options. Closes #9443 [sur]
* Limited eager loading no longer ignores scoped :order. Closes #9561 [danger, josh] * Limited eager loading no longer ignores scoped :order. Closes #9561 [danger, josh]

View file

@ -37,7 +37,7 @@ module ActiveRecord
# If using a custom finder_sql, scan the entire collection. # If using a custom finder_sql, scan the entire collection.
if @reflection.options[:finder_sql] if @reflection.options[:finder_sql]
expects_array = args.first.kind_of?(Array) expects_array = args.first.kind_of?(Array)
ids = args.flatten.compact.uniq ids = args.flatten.compact.uniq.map(&:to_i)
if ids.size == 1 if ids.size == 1
id = ids.first id = ids.first

View file

@ -535,6 +535,22 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) } assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
end end
def test_find_string_ids_when_using_finder_sql
firm = Firm.find(:first)
client = firm.clients_using_finder_sql.find("2")
assert_kind_of Client, client
client_ary = firm.clients_using_finder_sql.find(["2"])
assert_kind_of Array, client_ary
assert_equal client, client_ary.first
client_ary = firm.clients_using_finder_sql.find("2", "3")
assert_kind_of Array, client_ary
assert_equal 2, client_ary.size
assert_equal client, client_ary.first
end
def test_find_all def test_find_all
firm = Firm.find(:first) firm = Firm.find(:first)
assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length

View file

@ -38,6 +38,7 @@ class Firm < Company
has_many :no_clients_using_counter_sql, :class_name => "Client", has_many :no_clients_using_counter_sql, :class_name => "Client",
:finder_sql => 'SELECT * FROM companies WHERE client_of = 1000', :finder_sql => 'SELECT * FROM companies WHERE client_of = 1000',
:counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000' :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1'
has_many :plain_clients, :class_name => 'Client' has_many :plain_clients, :class_name => 'Client'
has_one :account, :foreign_key => "firm_id", :dependent => :destroy has_one :account, :foreign_key => "firm_id", :dependent => :destroy

View file

@ -159,8 +159,8 @@ class ReflectionTest < Test::Unit::TestCase
end end
def test_reflection_of_all_associations def test_reflection_of_all_associations
assert_equal 16, Firm.reflect_on_all_associations.size assert_equal 17, Firm.reflect_on_all_associations.size
assert_equal 14, Firm.reflect_on_all_associations(:has_many).size assert_equal 15, Firm.reflect_on_all_associations(:has_many).size
assert_equal 2, Firm.reflect_on_all_associations(:has_one).size assert_equal 2, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end end