On Sunday, September 4, 2016 at 4:59:39 PM UTC-7, Nick McFarlane wrote:
>
> Given the simple association of Author and Book as many_to_one in Sequel,
> I'm trying to generate a json output of the form:
>
> authors =>
> [
> { :id => 1,
> :book_ids => [1, 2, 3]
> },
> { :id => 2,
> :book_ids => [4, 5]
> }
> ]
>
>
>
> Using the Sequel models, I think this would be something like this:
>
> Authors.all.to_json( :include => { :books => { :only => :id } ) )
>
>
> However, I am trying to do it with only raw datasets. (The reason is that
> the database and hence models can change, so I would have to create and
> destroy the Model classes dynamically. The details of the associations are
> held in a separate database.)
>
> Is there a way this result be built manually?
>
Yes, there is. It's probably is a lot more code, though, enough that I
don't really want to spend the time to provide a example of how to do it.
Left as an exercise for the reader, so they say.
> Or can I define the association on the dataset without using models.
>
No, associations are a model level construction, not a dataset level one.
Note that it shouldn't be difficult to use models dynamically:
author = Sequel::Model(DB[:authors])
book = Sequel::Model(DB[:books])
author.one_to_many :books, :class=>book
author.plugin :json_serializer
authors.all.to_json( :include => { :books => { :only => :id } ) )
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.