From: Joe Van Dyk Date: 2006-01-26T02:08:49+09:00 Subject: Re: DRb and ActiveRecord On 1/25/06, Ezra Zygmuntowicz wrote: > Friends- > > I am trying to make a drb server publish an ActiveRecord model so it > can be used by remote clients. It looks like I am getting close but > no cigar. Can some kind soul enlighten me as to how to make this > work? It seems like it is communicating with the remote AR model. And > I am able to create records and find records. But I am unable to > display the contents of the records on the client side. It seems like > drb is not unmarshaling them correctly. Do I need to use DRbUndumped > or something similar? Any help is greatly appreciated! > > Here is my code and irb session: > # server.rb > > #!/usr/local/bin/ruby > require 'rubygems' > require 'drb/drb' > require_gem 'activerecord' > > ActiveRecord::Base.establish_connection( > :adapter => "mysql", > :username => "root", > :host => "localhost", > :password => "reversal", > :database => "switchboard_development" > ) > > class RemoteRecord < ActiveRecord::Base > end > > DRb.start_service("druby://:3500", RemoteRecord) > puts DRb.uri > DRb.thread.join > -------------------------------------- > And my irb session trying to access the remote model. > > ezra:~ ez$ irb > irb(main):001:0> require 'rubygems' > => true > irb(main):002:0> require 'drb/drb' > => true > irb(main):003:0> require_gem 'activerecord' > => true > irb(main):005:0* DRb.start_service > => # @idconv=#, @front=nil, @config= > {:idconv=># 0x410d88>, :argc_limit=>256, :verbose=>false, :tcp_acl=>nil, :load_limit > =>26214400}, @thread=#, @grp=# 0x137d374>, @protocol=## 0x410d88>, :argc_limit=>256, :verbose=>false, :tcp_acl=>nil, :load_limit > =>26214400}, @msg=# @load_limit=26214400>, @socket=#>> > > irb(main):006:0> test = DRbObject.new(nil, 'druby://ezra.local:3500') > => # > > irb(main):009:0> test.create(:title => 'Foo', :desc => 'Bar') > => # \"\010Bar", @name="RemoteRecord"> > > irb(main):011:0> a = test.find :first > => # \"\010bar", @name="RemoteRecord"> > > irb(main):012:0> p a > # \"\010bar", @name="RemoteRecord"> > => nil > > irb(main):013:0> a.title > NoMethodError: undefined method `title' for # > from (irb):13 > > > So you can see it is almost working! title and desc are columns in > the database table. The records do get created fine in the db and it > seems to get the right records when I run a find on the model. I just > can't figure out how to use the found object like a normal active > record. I mean I need to be able to display the attributes of the > record after I do a find and thats the part that is not working for me. Just a shot in the dark: does a.send :title work?