mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed Base#find to honor the documentation on how :joins work and make them consistent with Base#count #1405 [pritchie@gmail.com] Improved dynamic finder docs #1495 [laurel@gorgorg.org]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1510 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
3eed3272d7
commit
302c23d5a6
3 changed files with 23 additions and 6 deletions
|
@ -1,5 +1,17 @@
|
|||
*SVN*
|
||||
|
||||
* Fixed Base#find to honor the documentation on how :joins work and make them consistent with Base#count #1405 [pritchie@gmail.com]. What used to be:
|
||||
|
||||
Developer.find :all, :joins => 'developers_projects', :conditions => 'id=developer_id AND project_id=1'
|
||||
|
||||
...should instead be:
|
||||
|
||||
Developer.find(
|
||||
:all,
|
||||
:joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
|
||||
:conditions => 'project_id=1'
|
||||
)
|
||||
|
||||
* Fixed that validations didn't respecting custom setting for too_short, too_long messages #1437 [Marcel Molina]
|
||||
|
||||
* Fixed that clear_association_cache doesn't delete new associations on new records (so you can safely place new records in the session with Action Pack without having new associations wiped) #1494 [cluon]
|
||||
|
|
|
@ -135,9 +135,11 @@ module ActiveRecord #:nodoc:
|
|||
#
|
||||
# == Dynamic attribute-based finders
|
||||
#
|
||||
# Dynamic attribute-based finders are a cleaner way of getting objects by simple queries without turning to SQL. They work by
|
||||
# appending the name of an attribute to <tt>find_by_</tt>, so you get finders like <tt>Person.find_by_user_name, Payment.find_by_transaction_id</tt>.
|
||||
# So instead of writing <tt>Person.find(:first, ["user_name = ?", user_name])</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
|
||||
# Dynamic attribute-based finders are a cleaner way of getting objects by simple queries without turning to SQL. They work by
|
||||
# appending the name of an attribute to <tt>find_by_</tt> or <tt>find_all_by_</tt>, so you get finders like Person.find_by_user_name,
|
||||
# Person.find_all_by_last_name, Payment.find_by_transaction_id. So instead of writing
|
||||
# <tt>Person.find(:first, ["user_name = ?", user_name])</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
|
||||
# And instead of writing <tt>Person.find(:all, ["last_name = ?", last_name])</tt>, you just do <tt>Person.find_all_by_last_name(last_name)</tt>.
|
||||
#
|
||||
# It's also possible to use multiple attributes in the same find by separating them with "_and_", so you get finders like
|
||||
# <tt>Person.find_by_user_name_and_password</tt> or even <tt>Payment.find_by_purchaser_and_state_and_country</tt>. So instead of writing
|
||||
|
@ -737,7 +739,7 @@ module ActiveRecord #:nodoc:
|
|||
|
||||
def construct_finder_sql(options)
|
||||
sql = "SELECT * FROM #{table_name} "
|
||||
sql << ", #{options[:joins]} " if options[:joins]
|
||||
sql << " #{options[:joins]} " if options[:joins]
|
||||
add_conditions!(sql, options[:conditions])
|
||||
sql << "ORDER BY #{options[:order]} " if options[:order]
|
||||
add_limit!(sql, options)
|
||||
|
|
|
@ -292,8 +292,11 @@ class FinderTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_find_all_with_join
|
||||
developers_on_project_one = Developer.find :all, :joins => 'developers_projects', :conditions => 'id=developer_id AND project_id=1'
|
||||
|
||||
developers_on_project_one = Developer.find(
|
||||
:all,
|
||||
:joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
|
||||
:conditions => 'project_id=1'
|
||||
)
|
||||
assert_equal 2, developers_on_project_one.length
|
||||
developer_names = developers_on_project_one.map { |d| d.name }
|
||||
assert developer_names.include?('David')
|
||||
|
|
Loading…
Reference in a new issue