This project is archived and is in readonly mode.
Make "first" and "last" on associations accept conditions (etc)
-
Amos King
Maybe I'm missing something, but I put this test in my branch:
def test_first_on_association_with_conditions
firm = Firm.new("name" => "A New Firm, Inc")
firm.save
client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
client.save
client2 = Client.new("name" => "TheClient2.com", "firm_id" => firm.id)
client2.save
assert (firm.clients.first :conditions => { :name => 'TheClient2.com' }) == client2
end
and everything is passing just fine.
-
Amos King
Here is a patch that has passing tests to do the same thing. I don't think this a bug.
-
Wincent Colaiuta
Perhaps it depends on the type of the association and the options specified? I can reproduce this with a number of different models in my codebase. One example:
$ script/console Loading development environment (Rails 2.0.991) >> user = User.first => #<User id: 7921, display_name: "Wincent Colaiuta", passphrase_hash: "f2833a7d3dcd18b9c3c6379d461ad96586c5fb547c1c3dea6b2...", passphrase_salt: "1u1CfzMOxH4Jzguk", superuser: true, verified: true, suspended: false, session_key: "XHY8AUi08bjyxSpOkIvZ0eEE7ElWJOBg", session_expiry: "2008-04-18 20:54:50", deleted_at: nil, created_at: "2008-04-11 20:53:08", updated_at: "2008-04-11 20:54:50", comments_count: 4348, topics_count: 6> >> user.emails => [#<Email id: 6817, user_id: 7921, address: "win@wincent.org", default: true, verified: true, created_at: "2008-04-11 20:53:08", updated_at: "2008-04-11 20:53:08", deleted_at: nil>] >> user.emails.class => Array >> user.emails.first => #<Email id: 6817, user_id: 7921, address: "win@wincent.org", default: true, verified: true, created_at: "2008-04-11 20:53:08", updated_at: "2008-04-11 20:53:08", deleted_at: nil> >> user.emails.first :conditions => { :default => false } TypeError: can't convert Hash into Integer from /Users/wincent/trabajo/unversioned/wincent.com/src/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:177:in `first' from /Users/wincent/trabajo/unversioned/wincent.com/src/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:177:in `send' from /Users/wincent/trabajo/unversioned/wincent.com/src/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:177:in `method_missing' from /Users/wincent/trabajo/unversioned/wincent.com/src/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:250:in `method_missing' from (irb):5 >> user.emails.first(1) => [#<Email id: 6817, user_id: 7921, address: "win@wincent.org", default: true, verified: true, created_at: "2008-04-11 20:53:08", updated_at: "2008-04-11 20:53:08", deleted_at: nil>]Will see if I can come up with a reduced test case.
-
Wincent Colaiuta
Looks like this might be fixed by commit 73c5963 (which is post-RC1).
-
Pratik
- State changed from new to invalid
