Overriding default ActiveRecord behaviour

Hello everybody,

By default doing like this:

person = Person.new
person.name = “John”
person.save!

Will cause a new row to be inserted into database. But I don’t want it
to be saved into DB, instead when “save!” method is called I want to
connect to a particular URL and post data to it.
For instance:
POST http://my.app/people/create
“name” => “John”

I need this because I don’t have write access to my DB.

The obvious way to do it is override save, save!, create and delete
methods. but I wonder if there is more convenient way to do it.

Thanks!
Vlad.

Thanks!

If you need to do all interactions with your persistence layer via http,
this is a job for ActiveResource, not ActiveRecord.

If you just need to override the save method, then you can monkeypatch
by overriding the create_or_update method:

class Person < ActiveRecord::Base
def create_or_update
# do what you need to do here
end
end

Just make sure that you check the rails code for create_or_update so
that you know what functionality you’re replacing, so that your new
method will do what it is supposed to without breaking other save calls.