Make "first" and "last" on associations accept conditions (etc)
Reported by Wincent Colaiuta | May 29th, 2008 @ 06:16 PM
Just noticed that while you can do stuff like this:
Topic.first :conditions => { :foo => bar }
Trying the same type of thing across an association won't work:
topic.users.first :order => 'created_at'
topic.users.last :conditions => { :bar => baz }
topic.users.last :condtions => 'abc > 1'
All of those produce an exception:
TypeError: can't convert Hash into Integer
So evidently the proxy being returned by the association is Array-like (not sure of the internal implementation details, if it's an Array subclass, an Array extended with a module or something else), and the methods getting called are actually Array#first and Array#last, which take a single integer argument.
Not sure if this is a bug or merely a missing feature. I won't have time to dig into this myself for a while yet, but I wanted to get it into the tracker anyway so that it won't get forgotten.
Any comments on what the best solution would be? My guess is that we should alias the Array#first and Array#last methods, pass through to them if we get a single integer argument otherwise if we get a hash then do the standard ActiveRecord thing on them. What do people think?
Comments and changes to this ticket
-
Amos King May 29th, 2008 @ 10:14 PM
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 May 29th, 2008 @ 11:07 PM
- no changes were found...
-
Amos King May 30th, 2008 @ 11:49 AM
Here is a patch that has passing tests to do the same thing. I don't think this a bug.
-

Wincent Colaiuta May 30th, 2008 @ 11:08 AM
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 May 30th, 2008 @ 11:31 AM
Looks like this might be fixed by commit 73c5963 (which is post-RC1).
-
Pratik May 30th, 2008 @ 11:49 AM
- → State changed from new to invalid
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
