Overriding ActiveRecord::Base private methods

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

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.

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.

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

Chris,

Care to share some of what you have done?

Mike B.

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