[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

DRb and ActiveRecord

Ezra Zygmuntowicz

1/25/2006 4:52:00 PM

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
=> #<DRb::DRbServer:0x137d3b0 @uri="druby://ezra.local:61668",
@idconv=#<DRb::DRbIdConv:0x410d88>, @front=nil, @config=
{:idconv=>#<DRb::DRbIdConv:
0x410d88>, :argc_limit=>256, :verbose=>false, :tcp_acl=>nil, :load_limit
=>26214400}, @thread=#<Thread:0x137d324 sleep>, @grp=#<ThreadGroup:
0x137d374>, @protocol=#<DRb::DRbTCPSocket:0x137d02c @uri="druby://
ezra.local:61668", @acl=nil, @config={:idconv=>#<DRb::DRbIdConv:
0x410d88>, :argc_limit=>256, :verbose=>false, :tcp_acl=>nil, :load_limit
=>26214400}, @msg=#<DRb::DRbMessage:0x137d004 @argc_limit=256,
@load_limit=26214400>, @socket=#<TCPServer:0x137d0a4>>>

irb(main):006:0> test = DRbObject.new(nil, 'druby://ezra.local:3500')
=> #<DRb::DRbObject:0x1379990 @ref=nil, @uri="druby://ezra.local:3500">

irb(main):009:0> test.create(:title => 'Foo', :desc => 'Bar')
=> #<DRb::DRbUnknown:0x13727f8 @buf="\004\010o:\021RemoteRecord\010:
\020@new_recordF:\f@errorso:\031ActiveRecord::Errors\a;\a{\000:
\n@base@\000:\020@attributes{\010\"\ntitle\"\010Foo\"\aidi\010\"\tdesc
\"\010Bar", @name="RemoteRecord">

irb(main):011:0> a = test.find :first
=> #<DRb::DRbUnknown:0x136dce4 @buf="\004\010o:\021RemoteRecord\006:
\020@attributes{\010\"\ntitle\"\010foo\"\aid\"\0061\"\tdesc
\"\010bar", @name="RemoteRecord">

irb(main):012:0> p a
#<DRb::DRbUnknown:0x136dce4 @buf="\004\010o:\021RemoteRecord\006:
\020@attributes{\010\"\ntitle\"\010foo\"\aid\"\0061\"\tdesc
\"\010bar", @name="RemoteRecord">
=> nil

irb(main):013:0> a.title
NoMethodError: undefined method `title' for #<DRb::DRbUnknown:0x136dce4>
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.

Thoughts? Clues? Derision?

Thanks-
-Ezra





4 Answers

Joe Van Dyk

1/25/2006 5:09:00 PM

0

On 1/25/06, Ezra Zygmuntowicz <ezmobius@gmail.com> 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
> => #<DRb::DRbServer:0x137d3b0 @uri="druby://ezra.local:61668",
> @idconv=#<DRb::DRbIdConv:0x410d88>, @front=nil, @config=
> {:idconv=>#<DRb::DRbIdConv:
> 0x410d88>, :argc_limit=>256, :verbose=>false, :tcp_acl=>nil, :load_limit
> =>26214400}, @thread=#<Thread:0x137d324 sleep>, @grp=#<ThreadGroup:
> 0x137d374>, @protocol=#<DRb::DRbTCPSocket:0x137d02c @uri="druby://
> ezra.local:61668", @acl=nil, @config={:idconv=>#<DRb::DRbIdConv:
> 0x410d88>, :argc_limit=>256, :verbose=>false, :tcp_acl=>nil, :load_limit
> =>26214400}, @msg=#<DRb::DRbMessage:0x137d004 @argc_limit=256,
> @load_limit=26214400>, @socket=#<TCPServer:0x137d0a4>>>
>
> irb(main):006:0> test = DRbObject.new(nil, 'druby://ezra.local:3500')
> => #<DRb::DRbObject:0x1379990 @ref=nil, @uri="druby://ezra.local:3500">
>
> irb(main):009:0> test.create(:title => 'Foo', :desc => 'Bar')
> => #<DRb::DRbUnknown:0x13727f8 @buf="\004\010o:\021RemoteRecord\010:
> \020@new_recordF:\f@errorso:\031ActiveRecord::Errors\a;\a{\000:
> \n@base@\000:\020@attributes{\010\"\ntitle\"\010Foo\"\aidi\010\"\tdesc
> \"\010Bar", @name="RemoteRecord">
>
> irb(main):011:0> a = test.find :first
> => #<DRb::DRbUnknown:0x136dce4 @buf="\004\010o:\021RemoteRecord\006:
> \020@attributes{\010\"\ntitle\"\010foo\"\aid\"\0061\"\tdesc
> \"\010bar", @name="RemoteRecord">
>
> irb(main):012:0> p a
> #<DRb::DRbUnknown:0x136dce4 @buf="\004\010o:\021RemoteRecord\006:
> \020@attributes{\010\"\ntitle\"\010foo\"\aid\"\0061\"\tdesc
> \"\010bar", @name="RemoteRecord">
> => nil
>
> irb(main):013:0> a.title
> NoMethodError: undefined method `title' for #<DRb::DRbUnknown:0x136dce4>
> 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?


Bob Showalter

1/25/2006 5:23:00 PM

0

Ezra Zygmuntowicz wrote:
> Friends-

Advance warning: I am not a drb or ActiveRecord expert. I'm not much of
an expert on anything, come to think of it :-)

>
> I am trying to make a drb server publish an ActiveRecord model so
> it can be used by remote clients.
> ...
> class RemoteRecord < ActiveRecord::Base

You need to make this class undumped:

include DRb::DRbUndumped

> end
>
> DRb.start_service("druby://:3500", RemoteRecord)
> puts DRb.uri
> DRb.thread.join
> --------------------------------------
> And my irb session trying to access the remote model.
> ...
> irb(main):006:0> test = DRbObject.new(nil, 'druby://ezra.local:3500')
> => #<DRb::DRbObject:0x1379990 @ref=nil, @uri="druby://ezra.local:3500">
>
> irb(main):009:0> test.create(:title => 'Foo', :desc => 'Bar')
> => #<DRb::DRbUnknown:0x13727f8 @buf="\004\010o:\021RemoteRecord\010:
> \020@new_recordF:\f@errorso:\031ActiveRecord::Errors\a;\a{\000:
> \n@base@\000:\020@attributes{\010\"\ntitle\"\010Foo\"\aidi\010\"\tdesc
> \"\010Bar", @name="RemoteRecord">

DRbUnknown is your clue here. See:

http://www.ruby-doc.org/stdlib/libdoc/drb/rdoc/classes/DRb/DRbUn...

> ...
> irb(main):013:0> a.title
> NoMethodError: undefined method `title' for #<DRb::DRbUnknown:0x136dce4>
> from (irb):13

Since "a" is a DRbUnknown object, it doesn't have a title method; hence
the exception. You need drb to send over a proxy for the object so you
can send methods back to it. That's what DRb::DRbUndumped does. After
you make that change, "a" will be a DRb::DRbObject, and the method calls
will be sent back to the server.

Or something like that...


Ezra Zygmuntowicz

1/25/2006 5:33:00 PM

0

Bob-

Thank you very much. That was exactly what I needed and now it works
fine.

I appreciate it
-Ezra


On Jan 25, 2006, at 9:23 AM, Bob Showalter wrote:

> include DRb::DRbUndumped




Albert, Archi-druide français

4/11/2013 12:41:00 PM

0

Le 11/04/2013 13:23, poilagratter a ??crit :
> "Albert, Archi-druide fran??ais" <albert@france> wrote:
>> Le 10/04/2013 12:26, poilagratter a ??crit :
>>> "Albert, Archi-druide fran??ais" <albert@france> wrote:
>>>>
>>>>
>>>> Ici je vois la sauvegarde de 3.400 emplois, finalement le b??n??fice dans
>>>> cette affaire c'est clairement la France. Ce baraki oublie un peu trop
>>>> rapidement qu'il habite une r??gion qui ne garde la t??te hors de l'eau que
>>>> gr??ce au fond structurel europ??en et ?? l'argent venant de Flandre....
>>>
>>>
>>> 3400 emplois pour Boukaquistanais.
>>>
>>
>> L'H??tellerie Restauration des m??tiers pour "Boukaquistanais" !
>>
>> Quel m??pris vous avez pour des gens qui pratiquent ces professions
>> ??prouvantes. Verriez vous une objection ?? tenir les m??mes propos au
>> milieu d'un restaurant....je viendrais filmer..
>>
>
>
> Pousse la porte des cuisines et va voir qui travaille aux fourneaux, a la
> plonge etc.
> Va dans les grands h??tels voir qui fait les chambres, qui sert les p'tits
> d??j et qui est en salle pour les banquets.
>

Et apr??s ? Qu'est ce qui vous chiffonne ici ? Ils travaillent, je ne
vois pas ou est le probl??me..

--
Envoutement/d??senvoutement, retour d'affection, allongement du p??nis,
exorcisme,
r??pulsif anti franchouillards, filtre magique contre la cirrhose du foie
etc..