Hey Jeremy, thanks so much for this answer. After some fiddling around I 
got it to do what I wanted, but now I ran into another interesting side 
effect.

When it comes to limiting the results, I'd ideally like to limit by a total 
number of conversations and load however many messages that happened to 
include. IE: load three conversations, and if each conversation has 5 
messages or 50 messages, I don't really care.

When I use .limit on the end of that eager graph, it's limiting the total 
number of messages coming through, which I think I understand from a 
dataset point of view.

However, since that's not really the behavior I want, is there any way to 
limit on the number of conversations returned instead of the number of 
messages?

Thanks!
Andrew

On Monday, October 31, 2016 at 1:38:06 PM UTC-4, Jeremy Evans wrote:
>
> On Monday, October 31, 2016 at 3:49:22 PM UTC+1, Andrew Burleson wrote:
>>
>> I guess I don't really understand how `eager_graph` works. Looking 
>> through all the examples it seems you need to set up a relation and all 
>> eager_graph to load it, but in this case what I'm specifically interested 
>> in is generating a query that I can pass variables into, not hardcoded 
>> constraints.
>>
>
> eager_graph just returns a dataset where the tables have been joined, and 
> you can filter/order that dataset just as you would any other dataset.
>  
>
>> The behavior I want to write would be something like this pseudo code:
>>
>>
>> Conversation.with_messages_from_user_and_load_only_the_messages_from_this_user(user).all
>>
>> Where the output from the previous example would be:
>>
>> conversations #=> [Conversation[1], Conversation[2]]
>>
>> and if you looped like this:
>>
>> puts "Search results:"
>> conversations.each do |conv|
>>   puts "Conversation #{conv.id}"
>>   conv.messages.each{|m| puts "#{m.user.name}:\n#{m.text}" }
>>   puts "----"
>> end
>>
>> You'd get the following
>>
>> Search Result:
>> Conversation 1
>> User 1
>> Hello, World!
>> ----
>> Conversation 2
>> User 1
>> phone home
>> ----
>>
>> How would you do that with eager_graph?
>>
>
> conversations = 
> Conversation.eager_graph(:messages=>:user).where{{users[:id]=>1}}.all
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to