Overriding ActiveRecord::Base private methods

200 views
Skip to first unread message

snacktime

unread,
Oct 26, 2007, 12:42:01 PM10/26/07
to Ruby on Rails: Core
I'm struggling a bit with the best way to override the private create
and update methods. I have an adapter for a non sql backend and I
need to either override create and update, or alternatively maybe
override create_or_update and have it call replacements for create and
update. I would prefer the approach that modifies as little as
possible of activerecord.

Suggestions?

Chris

Michael Koziarski

unread,
Oct 26, 2007, 8:55:57 PM10/26/07
to rubyonra...@googlegroups.com

Given all the other things we do with SQL like .find(:all,
:include=>...) you're going to be changing a lot more than that.

My advice would be to aim to be duck-type compatible rather than reuse
the implementation. Take a look at how we've implemented
ActiveResource.
--
Cheers

Koz

snacktime

unread,
Oct 28, 2007, 1:52:47 AM10/28/07
to Ruby on Rails: Core

On Oct 26, 5:55 pm, "Michael Koziarski" <mich...@koziarski.com> wrote:
> > I'm struggling a bit with the best way to override the private create
> > and update methods. I have an adapter for a non sql backend and I
> > need to either override create and update, or alternatively maybe
> > override create_or_update and have it call replacements for create and
> > update. I would prefer the approach that modifies as little as
> > possible of activerecord.
>
> > Suggestions?
>
> Given all the other things we do with SQL like .find(:all,
> :include=>...) you're going to be changing a lot more than that.

Actually I want to keep most of the semantics of activerecord, I just
don't want to generate sql. With only overriding about 6 methods in
activerecord::base I have everything working except for some
associations. Right now what I'm doing is for our prototype, later
I'll probably write it clean from scratch as you suggest.

Chris

Mike Barsalou

unread,
Oct 28, 2007, 5:17:17 PM10/28/07
to Ruby on Rails: Core

Chris,

Care to share some of what you have done?

Mike B.

snacktime

unread,
Dec 14, 2007, 7:13:45 PM12/14/07
to Ruby on Rails: Core

>
> Care to share some of what you have done?
>
> Mike B.

Sorry it took so long to get back on this.

After a few iterations we basically have an async activeresource.
Inside activeresource we spin up a thread which opens a connection to
a localhost server and keeps the connection open across rails
requests, using a simple line based protocol. The localhost server
proxies the requests from activeresource into http requests to our
middleware. Since the localhost server uses Eventmachine, the whole
thing is asynchronous. For example, Person.find() returns
immediately, giving me an object that I can at any subsequent time
call a wait() method on when I need the results. Or optionally I can
just wait for Person.find() to return the result. It also lets us do
things like prefetch data for ajax requests that get called after the
page is loaded, etc.. We can also make activeresource calls in one
request, and then get the results in a subsequent rails request, by
saving query id's in the session.

I have permission to contribute this back as open source, but I need
another month or so of testing and adding a bit more abstraction
before I'm ready to release it. There are quite a few changes to
activeresource, primarily in connection.rb. I've also added in some
of the type checking/casting and validation helpers from activerecord,
and I'm working on some conventions for REST service discovery also.

Chris

Reply all
Reply to author
Forward
0 new messages