Search code examples
ruby-on-railsrubyactiverecordsinatradatamapper

Calling hooks on all applications connected to a database


I have two applications which connect to the same database.

I have written a database hook like so:

def call_side_effect(record)
  puts record
  # my application actually does something more complicated here
end

class Verb
  include DataMapper::Resource
  property :id,         Serial
  property :name,       String
  before(:save) { |record| call_side_effect(record) }
end

And this will print out the record attributes once it's created.

If I have this exact same hook defined in two applications (and they both connect to the same database), can I get it to run on both apps when one of them inserts a record?

I'm thinking of using Pusher, but I'm wondering if there is a way to do it using Datamapper or ActiveRecord by itself.

I'm using DataMapper and SQLite3 here, but could switch to a different ORM / db if needed.


Solution

  • Active Record's life-cycle events are implemented at the application level and thus will be localized to the application. I would suggest Datamapper works in a similar way. If you want two different applications to be alerted you will need some sort of external messaging system. I would suggest looking at RabbitMQ if you need a message bus. If you want to keep things on the database postgresql has a feature called NOTIFY(https://www.postgresql.org/docs/9.0/static/sql-notify.html) that might be of some use although I'm not sure how it plays with active record.