On Friday, April 19, 2019 at 12:12:39 AM UTC+9, shreko wrote:
>
> I like to use roda's :json plugin for sending JSON API Data where we just 
> send Array/Hash as output of a route. But, in order to take advantage of 
> Sequel Models and associations, I end up with arrays of models and 
> associated models.
> For example:
>
> plugin :json, :classes => [Array, Hash, Sequel::Model]
>
> r.get "eager_test" do
>   jobs = Job.where(:id => [100254,100255]).eager(:job_items).all
>   @rs[:jobs] = jobs.map do |v|
>     h = v.to_hash
>     h[:job_items] = v.job_items.map {|ji| ji.to_hash}
>     h
>      
>   end
>   @rs
> end
>
> This works as expected, but I am just wondering if there is a better way 
> to do it, without looping thru. Even though Sequel::Model is specified in 
> :json plugin options, it wouldn't convert Array of models to proper json 
> output, just pure models I guess.
>

Array#to_json should call #to_json on all elements of the array, so an 
array of Sequel::Model instances should work, as long as you use the Sequel 
json_serializer plugin in the related models.

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