extend Ambition to ActiveRecord associations
-
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
-
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.
-
Chris Wanstrath
- State changed from new to open
Grant, it might be even easier to accomplish this now.
Can you post your old code?
-
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.
-
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 :)
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.
