From: Jeremy Evans on
Phil Tayo wrote:
> Jeremy Evans wrote:
>> FYI, a Sequel dataset is an abstract representation of an SQL query, it
>> doesn't contain any records itself. Assuming that Array#to_json works
>> correctly, you could define:
>>
>> class Sequel::Dataset
>> def to_json
>> all.to_json
>> end
>> end
>>
>> and then call to_json on a dataset to get the query results in JSON
>> format.
>
> Thanks for that. A couple of problems I still have though:
>
> 1. I tried:
> ...
> Task.all.to_json
> ...
> and ".all" still returned an array of object references without the
> actual values, like this:
>
> ["#<Task:0x42527b4>","#<Task:0x4251800>","#<Task:0x425084c>","#<Task:0x424f898>","#<Task:0x424e8e4>","#<Task:0x424d930>","#<Task:0x424c97c>"]
>
> 2. Once that's working, where should I put this code:
>> class Sequel::Dataset
>> def to_json
>> all.to_json
>> end
>> end
> Can you point me in the direction of any useful reading material?

If your dataset is a model dataset, you probably don't want model
objects. Try this more general version:

class Sequel::Dataset
def to_json
naked.all.to_json
end
end

The naked part means that instead of model objects, plain hashes are
returned.

Jeremy
--
Posted via http://www.ruby-forum.com/.

From: Phil Tayo on

> class Sequel::Dataset
> def to_json
> naked.all.to_json
> end
> end
>
> The naked part means that instead of model objects, plain hashes are
> returned.
>
> Jeremy

That's brilliant thanks Jeremy, it's working now.

Now my problem is with the to_json function. I have a created_at field
in the DB of type datetime and the to_json function seems to be
splitting it up and formatting it weirdly, here's the result of my json
object:

[{"country":"uk","task":"qwdwd","created_at":{"json_class":"Time","n":407000000,"s":1266543411},"id":3}]

I'll have to look into this when I have more time.
--
Posted via http://www.ruby-forum.com/.

From: Jeremy Evans on
Phil Tayo wrote:
> Now my problem is with the to_json function. I have a created_at field
> in the DB of type datetime and the to_json function seems to be
> splitting it up and formatting it weirdly, here's the result of my json
> object:
>
> [{"country":"uk","task":"qwdwd","created_at":{"json_class":"Time","n":407000000,"s":1266543411},"id":3}]
>
> I'll have to look into this when I have more time.

You probably want to add Time#to_json to do something, maybe
to_s.to_json or strftime(...).to_json.

Jeremy
--
Posted via http://www.ruby-forum.com/.

From: Phil Tayo on
[{"country":"uk","task":"qwdwd","created_at":{"json_class":"Time","n":407000000,"s":1266543411},"id":3}]
>>
>> I'll have to look into this when I have more time.
>
> You probably want to add Time#to_json to do something, maybe
> to_s.to_json or strftime(...).to_json.
>
> Jeremy

Thanks but when I do this: Task.naked.all.to_s.to_json it converts the
whole object to one big string. I'm not sure how to specify to to_s for
only one field of my object i.e. "created_at".
--
Posted via http://www.ruby-forum.com/.

From: Jeremy Evans on
Phil Tayo wrote:
> [{"country":"uk","task":"qwdwd","created_at":{"json_class":"Time","n":407000000,"s":1266543411},"id":3}]
>>>
>>> I'll have to look into this when I have more time.
>>
>> You probably want to add Time#to_json to do something, maybe
>> to_s.to_json or strftime(...).to_json.
>>
>> Jeremy
>
> Thanks but when I do this: Task.naked.all.to_s.to_json it converts the
> whole object to one big string. I'm not sure how to specify to to_s for
> only one field of my object i.e. "created_at".

I assume that you are fairly new to ruby, so I'll spell it out for you:

class Time
def to_json
to_s.to_json
end
end
class Sequel::Dataset
def to_json
naked.all.to_json
end
end
class Sequel::Model
def self.to_json
dataset.to_json
end
end
Task.to_json

Jeremy
--
Posted via http://www.ruby-forum.com/.