Hi,
I’m trying to use ActiveRecord objects through Drb, and I’m facing a
strange thing:
Firstly, I’m using the following database:
– SQL
CREATE TABLE gardens
(
lock_version integer DEFAULT 0,
id integer NOT NULL AUTO_INCREMENT,
name varchar(50),
PRIMARY KEY (id)
) TYPE=INNODB;
CREATE TABLE flowers
(
lock_version integer DEFAULT 0,
id integer NOT NULL AUTO_INCREMENT,
garden_id integer NOT NULL,
name varchar(50),
color varchar(50),
PRIMARY KEY (id),
FOREIGN KEY (garden_id) REFERENCES gardens (id)
) TYPE=INNODB;
Now if I launch this server code:
– Drb / ActiveRecord Server
#!/usr/local/bin/ruby
require ‘rubygems’
require ‘drb’
require ‘active_record’
require ‘active_record/transactions’
ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
:username => “root”,
:host => “localhost”,
:password => “”,
:database => “flowers”
)
We build the ActiveRecord objects
class Garden < ActiveRecord::Base
include DRbUndumped
has_many :flowers
end
class Flower < ActiveRecord::Base
include DRbUndumped
belongs_to :garden
end
We add some data
Garden.transaction do
g = Garden.new(“name” => “garden1”)
g.flowers << Flower.new(“name” => “flower1”, “color” => “color1”)
g.flowers << Flower.new(“name” => “flower2”, “color” => “color2”)
g.save
g = Garden.new(“name” => “garden2”)
g.flowers << Flower.new(“name” => “flower3”, “color” => “color3”)
g.save
end
We create the server and instanciate it
class Server
def find(i)
Garden.find(i)
end
end
s = Server.new
We query the server
g = s.find(1)
puts “#{g.id} | #{g.name}”
g.flowers.each do |f|
puts “#{f.id} | #{f.garden_id} | #{f.name}”
end
We make the server available thruogh Drb
DRb.start_service(‘druby://localhost:9000’, s)
DRb.thread.join
… I get what I was expecting, that is:
1 | garden1
1 | 1 | flower1
2 | 1 | flower2
Now I’m trying to call the server through Drb, with exactly the same
code:
– Drb / ActiveRecord Client, version 1
#!/usr/local/bin/ruby
require ‘drb’
DRb.start_service()
obj = DRbObject.new(nil, ‘druby://localhost:9000’)
g = obj.find(1)
puts “#{g.id} | #{g.name}”
g.flowers.each do |f|
puts “#{f.id} | #{f.garden_id} | #{f.name}”
end
… I get:
ar_client.rb:8: warning: Object#id will be deprecated; use
Object#object_id
67884862 | garden1
ar_client.rb:10: warning: Object#id will be deprecated; use
Object#object_id
67882632 | 1 | flower1
ar_client.rb:10: warning: Object#id will be deprecated; use
Object#object_id
67880122 | 1 | flower2
Where do these warnings come from? And why these ids?
II follow the warning message, and change the code a little bit:
– Drb / ActiveRecord Client, version 2
#!/usr/local/bin/ruby
require ‘drb’
DRb.start_service()
obj = DRbObject.new(nil, ‘druby://localhost:9000’)
g = obj.find(1)
puts “#{g.object_id} | #{g.name}”
g.flowers.each do |f|
puts “#{f.object_id} | #{f.garden_id} | #{f.name}”
End
… I get:
67884862 | garden1
67882642 | 1 | flower1
67880142 | 1 | flower2
Warning disappeared, but Ids are still wrong.
Does anyone understand what happens here?
Philippe L., Ing. Dipl. EPFL
Attik System
rte de la Fonderie 2
1700 Fribourg
Switzerland
http://www.attiksystem.ch
Tel: +41 (26) 422 13 75
Fax: +41 (26) 422 13 76