Below is a sample script and exit.
Two typical models, cities and countries. Need to get json data from
all the cities of the country involved.
According to the documentation (http://sequel.rubyforge.org/rdoc-
plugins/classes/Sequel/Plugins/JsonSerializer.html), the output should
be JSON with nested structures. But in this example, the output array
of hashes, not attachments. This is the correct behavior of the plug-
in serializer?
### script.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'mysql2'
require 'sequel'
require 'logger'
require 'pp'
db = Sequel.connect( "mysql2://root@localhost/test4" )
db.loggers << Logger.new($stdout)
db.create_table :countries do
primary_key :id
String :name
end
db.create_table :cities do
primary_key :id
String :label
foreign_key :country_id, :countries
end
Sequel::Model.plugin :json_serializer
class City < Sequel::Model
many_to_one :country
end
class Country < Sequel::Model
one_to_many :cities
end
country = Country.create(:name => "country_name_1")
City.create( :label => 'C1', :country => country)
City.create( :label => 'C2', :country => country)
all_cities = City.eager_graph(:country).all
puts all_cities.to_json(:include => :country)
### output
I, [2012-03-07T14:26:51.411320 #2710] INFO -- : (0.000212s) SET
@@wait_timeout = 2147483
I, [2012-03-07T14:26:51.411544 #2710] INFO -- : (0.000096s) SET
SQL_AUTO_IS_NULL=0
I, [2012-03-07T14:26:51.461163 #2710] INFO -- : (0.049441s) CREATE
TABLE `countries` (`id` integer PRIMARY KEY AUTO_INCREMENT, `name`
varchar(255))
I, [2012-03-07T14:26:51.463477 #2710] INFO -- : (0.001736s) DESCRIBE
`countries`
I, [2012-03-07T14:26:51.660853 #2710] INFO -- : (0.196214s) CREATE
TABLE `cities` (`id` integer PRIMARY KEY AUTO_INCREMENT, `label`
varchar(255), `country_id` integer, FOREIGN KEY (`country_id`)
REFERENCES `countries`(`id`))
I, [2012-03-07T14:26:51.673743 #2710] INFO -- : (0.002116s) DESCRIBE
`cities`
I, [2012-03-07T14:26:51.676713 #2710] INFO -- : (0.000225s) BEGIN
I, [2012-03-07T14:26:51.677407 #2710] INFO -- : (0.000330s) INSERT
INTO `countries` (`name`) VALUES ('country_name_1')
I, [2012-03-07T14:26:51.677966 #2710] INFO -- : (0.000183s) SELECT *
FROM `countries` WHERE (`id` = 1) LIMIT 1
I, [2012-03-07T14:26:51.678411 #2710] INFO -- : (0.000320s) COMMIT
I, [2012-03-07T14:26:51.679109 #2710] INFO -- : (0.000073s) BEGIN
I, [2012-03-07T14:26:51.679590 #2710] INFO -- : (0.000185s) INSERT
INTO `cities` (`label`, `country_id`) VALUES ('C1', 1)
I, [2012-03-07T14:26:51.680121 #2710] INFO -- : (0.000221s) SELECT *
FROM `cities` WHERE (`id` = 1) LIMIT 1
I, [2012-03-07T14:26:51.680566 #2710] INFO -- : (0.000322s) COMMIT
I, [2012-03-07T14:26:51.680863 #2710] INFO -- : (0.000051s) BEGIN
I, [2012-03-07T14:26:51.681178 #2710] INFO -- : (0.000111s) INSERT
INTO `cities` (`label`, `country_id`) VALUES ('C2', 1)
I, [2012-03-07T14:26:51.681549 #2710] INFO -- : (0.000128s) SELECT *
FROM `cities` WHERE (`id` = 2) LIMIT 1
I, [2012-03-07T14:26:51.682109 #2710] INFO -- : (0.000465s) COMMIT
I, [2012-03-07T14:26:51.683121 #2710] INFO -- : (0.000306s) SELECT
`cities`.`id`, `cities`.`label`, `cities`.`country_id`, `country`.`id`
AS `country_id_0`, `country`.`name` FROM `cities` LEFT OUTER JOIN
`countries` AS `country` ON (`country`.`id` = `cities`.`country_id`)
[{"json_class":"City","id":1,"label":"C1","country_id":1},
{"json_class":"City","id":2,"label":"C2","country_id":1}]
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.