#106 open
Grant Rodgers

extend Ambition to ActiveRecord associations

Reported by Grant Rodgers | September 6th, 2007 @ 05:07 PM

Seems like a natural addition: add all the goodness from ActiveRecord classes to associations!

We have this kind of stuff all over our app:

current_user.answers.first(5)

current_user.experiences.size

Not a problem at first, but when associations start getting up to thousands of records, that kind of thing can get really slow really quick. Kicker methods would clean it all up real nice.

Comments and changes to this ticket

  • ronin-13570 (at lighthouseapp)

    ronin-13570 (at lighthouseapp) February 15th, 2008 @ 03:09 AM

    Can you elaborate on this further? I'd be more than happy to have a look at doing the work.

    Cheers,

    Simon

  • Grant Rodgers

    Grant Rodgers February 15th, 2008 @ 07:53 PM

    ActiveRecord associations already support Enumerable methods, but they're useless in large scale apps because the entire collection is loaded, and you could have a collection with thousands of records.

    Currently you don't want to write code like "current_user.experiences.size" because of the collection loading. You have to use the special purpose method count. To get "experiences.first(5)" without loading the entire collection you have to use "current_user.experiences.find(:all, :limit => 5)" or a paginator.

    But if we change association reader methods to return Ambition query contexts, then running enumerable methods on them doesn't load the entire collection, and we can use associations like normal arrays.

    When I filed the ticket I had a semi-working version of this feature, but Ambition has changed alot since then so I doubt it works anymore.

  • Chris Wanstrath

    Chris Wanstrath February 16th, 2008 @ 03:32 AM

    • State changed from “new” to “open”

    Grant, it might be even easier to accomplish this now.

    Can you post your old code?

  • Grant Rodgers

    Grant Rodgers February 16th, 2008 @ 05:01 PM

    Pretty simple really, I just replaced the contents of the define_method block in collection_reader_method with

    reflection.klass.query_context.select { |entity| entity.__send__(reflection.primary_key_name) == quoted_id }

    diff attached.

  • Ben Hoskings

    Ben Hoskings August 8th, 2008 @ 08:40 PM

    • Tag set to ambition

    I'm really interested in investigating this stuff further; I'm working on a Rails plugin at the moment that is perfect for exploiting this. Arbitrary activerecord associations within the block would be terribly pro.

    Haven't got time to dig into the code right now but I thought I'd just throw my hat into the ring. Cheers :)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

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

Shared Ticket Bins

People watching this ticket

Tags

Pages