On Apr 1, 3:29 am, David Lee <[email protected]> wrote:
> Hey Jeremy,
>
> I wanted to use eager or eager_graph to easily fetch and cache a non-
> predefined association. The thing I want to speed up is this:
>
>   Post.each do |post|
>     post.comments.filter(:user_id => params[:id]).first
>   end
>
> As you can see, I need to filter and limit the associated table
> dynamically (against some argument, in this case params[:id]).
>
> I feel like there would be something to the effect of:
>
>   Post.eager_graph(:comments).eager_filter(:user_id => params
> [:id]).eager_limit(1)
>
> but I can't find it.
>
> Can you help me with this?

  Post.eager_graph(:comments).filter(:comments__user_id=>params
[:id]).all

should limit the returned Posts to ones that have comments where the
user_id is the same as params[:id].  There's no way to set a limit
when you are eager loading, though.  You might be able to hack
something together using distinct on, but your database would need to
support that.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to