On Friday, August 31, 2018 at 2:25:30 PM UTC-7, Elanor Riley wrote:
>
> I'm experiencing some weird behavior with to_json and eager loading and
> eager graph.
>
> Everything works well with eager loaded relationships and to_json on a
> model, for instance
>
> Order.eager(:invoice, :confirmations, :user)
> .to_json(
> {
> :except => [:sales_line, :table_dlv_addr],
> :include => {
> :user => {:only => :name},
> :invoice => {:only => :id},
> :confirmations => {:only => :id}
> }
> }
> )
>
> // Resulting JSON data
> {
> confirmations:[{id: 4873}]
> created_at:"2017-06-23 08:40:41 -0400"
> document_status:"invoice"
> id:5135
> invoice:{id: 9698}
> notes:""
> process:true
> receipt_date_requested:"2017-08-31"
> sales_id:"SO00007950"
> status:"approved"
> total_weight:42374
> updated_at:"2017-09-06 00:09:02 -0400"
> user:{name:"John Doe"}
> user_id:138
> }
>
>
> However, our project requirements have changed and I now need to be able
> to select and filter based on the User data (for example):
>
> Order.eager_graph(:user) # Need to query user data
> .eager(:invoice, :confirmations)
> .where('user.name = "John Doe"') # Need to query user data
> .to_json(
> {
> :except => [:sales_line, :table_dlv_addr],
> :include => {
> :user => {:only => :name},
> :invoice => {:only => :id},
> :confirmations => {:only => :id}
> }
> }
> )
>
> The JSON response now only includes order attributes, and my exclusions
> are completely ignored, and the associated eager loaded relationships are
> not included (though queries are still run to get their results)
>
> {
> // Confirmations missing
> created_at:"2017-06-23 08:40:41 -0400"
> document_status:"invoice"
> id:5135
> // Invoice data missing
> notes:""
> process:true
> receipt_date_requested:"2017-08-31"
> sales_id:"SO00007950"
> sales_lines: [...] // Supposed to be excluded
> status:"approved"
> table_dlv_addr: {...} // supposed to be excluded
> total_weight:42374
> updated_at:"2017-09-06 00:09:02 -0400"
> // User Data missing
> user_id:138
> }
>
>
That does look strange. json_serializer is supposed to work with
eager_graph, and there are specs that test that. Could you put together a
minimal, self contained, reproducible example that I can test with (or add
a failing spec/integration test)?
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.