On Monday, January 13, 2014 7:06:59 AM UTC-8, tony wrote:
>
> Hi
>
>
> I would like to know if there is a good way with minimal code to do a
> query and transform to an array.
> if I take the example Artist to Album. 1 Artist to many Albums . When I do
> the query
>
> artist = Artist.graph(:Albums, {:artist_id => :id}, :join_type =>
> :inner).where(:album__sold = 21)
> and when I do
> puts artist.to_json
>
> I would like the results to be:
> [
> ...,
> { "id" : 1,
> "name" : "artist name",
> "age" : 20,
> "Albums":[ {:id = 1, "name":"album_name", "sold":21}, {:id = 4,
> "name":"album_name", "sold":21} ]
> },
> .....
> ]
>
> At the moment I have to code and make sure I generate a json using the
> columns in each model
> e.g..
> this is an example loop
> result.each do |row|
> :id = row[:id]
> Artist.columns.each do |cl_name|
> my_artist[:id][:cl_name] = row[:cl_name]
> end
> Album.columns.each do |cl_name|
> my_album[:cl_name] = row[:cl_name]
> end
> my_artist[:id][:albums] << my_album # not totally correct but something
> like that.
> etc.......
>
>
>
> I was hoping to just do artist.to_json or something similar.
>
Look into the json_serializer plugin.
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/groups/opt_out.