This project is archived and is in readonly mode.

#275 ✓invalid
Wincent Colaiuta

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

    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
  • Amos King

    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

    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

    Wincent Colaiuta May 30th, 2008 @ 11:31 AM

    Looks like this might be fixed by commit 73c5963 (which is post-RC1).

  • Pratik

    Pratik May 30th, 2008 @ 11:49 AM

    • State changed from “new” to “invalid”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages