On Thursday, August 28, 2014 12:22:10 PM UTC-7, Paul Carew wrote:
>
> Hi
>
> Hopefully this is an easy one but one I've been puzzling over it for a 
> while now...
>
> When including associations with the json_serializer is it possible to 
> modify the association criteria?
>
> For example if I have:
>
>   Episode.where(:user_id => user).to_json(:include => :colleagues)
>
> How might I go about including only those :colleagues that have a boolean 
> column named "active" set to true?
>
> Using .join() I understandably get NoMethodErrors on the join columns 
> when doing to_json, as there are no corresponding instance methods on 
> Episode.
>
> Is there a way around this?
>

I'm not sure the API currently supports something like that.  We could 
expand the API with additional options to support it, but it's already a 
fairly complex API.  I suppose the natural way to handle it if we were to 
expand the API would be:

  Episode.where(:user_id => user).to_json(:include 
=>{:colleagues=>{:callback=>proc{|ds| ds.where(:active)}}})

The problem is that is already valid and used to pass the :callback option 
to the Colleague#to_json method.

You could have an active_colleagues association that just returns active 
colleagues, but that's probably not going to give you the json you want 
(using active_colleagues as the key instead of colleagues).  We could 
support an additional option like :key_name to work around that, but it has 
the same issues as adding :callback.

For advanced use cases, you may be better off using a different tool, such 
as rabl.

I'm open to feedback in terms of expanding json_serializer options, so if 
anyone has opinions, please speak up.

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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to