If I use :include to eager load a has_many association, can I limit the number returned on that has_many association. I know I can limit the amount of records returned, but how do I limit how many are returned on its association? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi,> If I use :include to eager load a has_many association, can I limit the > number returned on that has_many association. I know I can limit the > amount of records returned, but how do I limit how many are returned on > its association?If there is a way of limiting the includes explicitely when using the finder, I''m not aware of it.But you can use :limit as one of the options when you are defining the has_many association itself at the model, so you could so something like: has_many :detail_items, :limit=>10 There are also a number of other options you can use to further selecting which of the detail_items will be retrieved for this association, like :order or :select. Regards, javier ramirez --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ActiveRecord can''t currently do that, but it''d be a nice feature given how difficult it is to customize the eager loading queries. However, you might be able emulate the functionality by constructing your own query to fetch the ids of the association that match. Then you could put them in a big array and pass them into the conditions on the final query. There would be a lot of issues, and it might ultimately be slower than the n+1 queries. There''s no direct way to limit the secondary results although you might be able to cook something up with subselects or other advanced db features. On May 15, 4:03 pm, Aryk Grosz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> If I use :include to eager load a has_many association, can I limit the > number returned on that has_many association. I know I can limit the > amount of records returned, but how do I limit how many are returned on > its association? > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I was thinking about that Javier, but I think it would ignore the :limit when it gets eager loaded. Have you heard of it working? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aryk Grosz wrote:> I was thinking about that Javier, but I think it would ignore the :limit > when it gets eager loaded. >to be honest i assumed it would work when eager loading but didn''t try before. I''m successfully using it for a model in which i''m not using eager-loading (just for a combo with ajax, so no need for the details beforehand) and I thought it would be just fine when including. I just tested that on the console and you are right, it will totally ignore the limit option in that case. I understand the sql for limiting on the association would get a bit tricky, but I was hoping AR would apply the limit when mapping the results. Unfortunately it''s not. regards, javier --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---