Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

extend Ambition to ActiveRecord associations

#106

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.

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

State: open
Milestone: none
Assigned to: Chris Wanstrath Chris Wanstrath

Activity

  1. ronin-13570 (at lighthouseapp)
    ronin-13570 (at lighthouseapp)

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

    Cheers,

    Simon

    February 15th, 2008 @ 03:09 AM

  2. Grant Rodgers
    Grant Rodgers

    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.

    February 15th, 2008 @ 07:53 PM

  3. Chris Wanstrath
    Chris Wanstrath
    • State changed from new to open

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

    Can you post your old code?

    February 16th, 2008 @ 03:32 AM

  4. Grant Rodgers
    Grant Rodgers

    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.

    February 16th, 2008 @ 05:01 PM

  5. Ben Hoskings
    Ben Hoskings
    • 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 :)

    August 8th, 2008 @ 08:40 PM

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.