I've also found myself doing that :preload_associations trick from
time to time and I'm similarly thinking that that shows two things:
1) Maybe we should make a nicer way for people to do this (fred cheung
was playing with an array proxy to enable this kind of thing)
2) We might want to make :strategy=>:preload an option to find.
I've no idea if :strategy is a good name for the option, but I think
it's probably a good idea to investigate whether the option is
feasible and if it would be useful.
Care to take a crack at it?
--
Cheers
Koz
> One thought I had about :strategy => :preload is that compared to the
> current hacky approach it's a bit less flexible, because it doesn't
> let you mix and match. Given that drawback, I wonder if its even
> worth complicating the API. Maybe officially
> making :preload_associations public and documenting it would be worth
> it?
I was thinking the same thing. I find preload_associations to be a
useful tool to work with directly. +1 to making it (or something
similar) public and documented.
Sounds good to me, but I also like the idea of being able to do something like
@orders = Order.find_by_sql(CRAZY_SHIT)
@orders.preload(:line_items=>{:sku=>:title})
--
Cheers
Koz
I like this approach. :include should definitely preload, and people should have to use :joins and :left_joins when they need them all in the same query.What would :right_joins do though? You need a left record to load associations of, otherwise how would you get to the right objects?
Regarding making people explicitly specify the required joins to put conditions on the associations, if I understand you correctly, I think this would tend to mean that you end up with numerous copies of your basic foreign key-based join conditions throughout the codebase, which is not very DRY.
Not really. You can already specify associations in :joins arguments. So your example would be :
Lunch.find(:all, :left_joins => :pies, :conditions => {:pies => {:steak => true}})
With that you mean that it's not going to interpret the select, order
and condition strings anymore to determine if they're referencing some
table? I'd be all for that! The interpretation of those string
currently uses a too simple heuristic and is therefor just not working
in practice.
>> - Introduce :left_joins and :right_joins keys to the finder. Title says it
>> all
>>
+1. Though I prefer the terms :left_outer_joins and :right_outer_joins.
For completeness sake also introduce :full_outer_joins.
>> - Make user explicitly specify the required joins if they want to put
>> conditions on the associations
>> - For all the associations specified in :include, check if the required
>> dataset has already been loaded by :*joins. If not, preload.
>>
I guess this means that the :joins method will no longer accept a
string? (or if they will, then a string value will be ignored to
determine if preloading must occur or not)
> Sounds good to me, but I also like the idea of being able to do something like
>
> @orders = Order.find_by_sql(CRAZY_SHIT)
> @orders.preload(:line_items=>{:sku=>:title})
>
+1 on a public preload method as well.
Cheers,
Lawrence
With that you mean that it's not going to interpret the select, order
>> - Make :include *always* use preload strategy unless the required data set
>> is explicitly loaded
>>
and condition strings anymore to determine if they're referencing some
table? I'd be all for that! The interpretation of those string
currently uses a too simple heuristic and is therefor just not working
in practice.
+1. Though I prefer the terms :left_outer_joins and :right_outer_joins.
>> - Introduce :left_joins and :right_joins keys to the finder. Title says it
>> all
>>
For completeness sake also introduce :full_outer_joins.
I guess this means that the :joins method will no longer accept a
>> - Make user explicitly specify the required joins if they want to put
>> conditions on the associations
>> - For all the associations specified in :include, check if the required
>> dataset has already been loaded by :*joins. If not, preload.
>>
string? (or if they will, then a string value will be ignored to
determine if preloading must occur or not)