Well, .to_json works fine when you don't need the extra parameters for
normal data table usage.

But the server side processing requirements facilitate the need for those
extra attributes. I was hoping to be able to just *insert* them somehow
while also utilizing the nice function of .to_json by default. Especially
because the query I'm using has some complex associations.

On Jun 14, 2017 10:13 AM, "Jeremy Evans" <[email protected]> wrote:

> On Wednesday, June 14, 2017 at 5:38:08 AM UTC-7, Elanor Riley wrote:
>>
>> Hey guys, I'm glad that this has gotten a robust discussion going. I'm
>> not sure I fully explained what I actually need to be done.
>>
>> I'm using the jQuery datatables plugin, and for server-side filtering, it
>> requires some extra data in the JSON extract that doesn't come from a
>> .to_json call on a Sequel data object.
>>
>> For example, dataTables expects a structure like the following:
>>
>> {
>>  "draw": 1,
>>  "recordsTotal": 57,
>>  "recordsFiltered": 57,
>>  "data": [
>>    [
>>      "Airi",
>>      "Satou",
>>      "Accountant",
>>      "Tokyo",
>>      "28th Nov 08",
>>      "$162,700"
>>    ],
>>    [
>>      "Angelica",
>>      "Ramos",
>>      "Chief Executive Officer (CEO)",
>>      "London",
>>      "9th Oct 09",
>>      "$1,200,000"
>>    ]
>>  ]
>> }
>>
>> In this case, the "data" attribute is the set of objects returned by
>> Sequel, but the other attributes need to be manually created or set. Is
>> there a way for me to return JSON like this while using .to_json?
>>
>> I've tried something like the following:
>> @data = Model.where(:attribute => true) #Filtered based on POST data
>> from datatables
>>
>> {:recordsTotal => Model.all.count, :recordsFiltered => @data.count, :data
>> => @data.to_json}.to_json
>>
>> But that resulted in double escaped JSON that didn't parse.
>>
>>
> This representation is totally different than the one that Model#to_json
> will produce.  For one, this returns data as an array instead of a hash.
> You are better off adding your own method for this rather than trying to
> get  to_json to work.
>
> Thanks,
> Jeremy
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "sequel-talk" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/sequel-talk/FOQ_09mizKw/unsubscribe.
> To unsubscribe from this group and all its topics, 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.
>

-- 
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