Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3463

Show
Ignore:
Timestamp:
01/22/06 09:34:41 (3 years ago)
Author:
marcel
Message:

Make dynamic finders honor additional passed in :conditions. Closes #3569.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r3460 r3463  
    11*SVN* 
     2 
     3* Make dynamic finders honor additional passed in :conditions. #3569 [Oleg Pudeyev <pudeyo@rpi.edu>, Marcel Molina Jr.] 
    24 
    35* Show a meaningful error when the DB2 adapter cannot be loaded due to missing dependencies. [Nicholas Seckar] 
  • trunk/activerecord/lib/active_record/base.rb

    r3439 r3463  
    991991            conditions = construct_conditions_from_arguments(attribute_names, arguments) 
    992992 
    993             if arguments[attribute_names.length].is_a?(Hash) 
    994               find(finder, { :conditions => conditions }.update(arguments[attribute_names.length])) 
     993            if (extra_options = arguments[attribute_names.size]).is_a?(Hash) 
     994              finder_options = extra_options.merge(:conditions => conditions) 
     995              if extra_options[:conditions] 
     996                with_scope(:find => {:conditions => extra_options[:conditions]}) do 
     997                  find(finder, finder_options) 
     998                end 
     999              else 
     1000                find(finder, finder_options) 
     1001              end 
    9951002            else 
    9961003              send("find_#{finder}", conditions, *arguments[attribute_names.length..-1]) # deprecated API 
  • trunk/activerecord/test/finder_test.rb

    r3382 r3463  
    77 
    88class FinderTest < Test::Unit::TestCase 
    9   fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts 
     9  fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :accounts 
    1010 
    1111  def test_find 
     
    201201  end 
    202202 
     203  def test_find_by_one_attribute_with_order_option 
     204    assert_equal accounts(:signals37), Account.find_by_credit_limit(50) 
     205    assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :order => 'id DESC') 
     206  end 
     207 
     208  def test_find_by_one_attribute_with_conditions 
     209    assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6]) 
     210  end 
     211 
     212  def test_find_by_one_attribute_with_several_options 
     213    assert_equal accounts(:unknown), Account.find_by_credit_limit(50, :order => 'id DESC', :conditions => ['id != ?', 3]) 
     214  end 
     215   
    203216  def test_find_by_one_missing_attribute 
    204217    assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") } 
  • trunk/activerecord/test/fixtures/accounts.yml

    r2595 r3463  
    1212  firm_id: 6 
    1313  credit_limit: 50 
     14 
     15last_account: 
     16  id: 4 
     17  firm_id: 2 
     18  credit_limit: 60