简体   繁体   中英

how to implement ruby on rails Observer for remote database

I have a requirement to observe a remote database table. I am looking at code like the following:

class RemotetableObserver < ActiveRecord::Observer

  # Need to watch the remote table
  ActiveRecord::Base.establish_connection "remoteDB"
  observe :remotetable 

  def after_create(row)
    doStuff.create(row)
  end
end

I have added

config.active_record.observers = :remotetable_observer

to my application.rb config file, and my database.yml connects to the remote database.

I am getting NameError: uninitialized constant remotetable so I created a further model:

# remotetable.rb   
class Remotetable < ActiveRecord::Base
  # establish_connection(ActiveRecord::Base.configurations["otherdb_#{RAILS_ENV}"])
  ActiveRecord::Base.establish_connection "remoteDB"
  self.table_name = "remotetable"
end

but still getting the same error: NameError: uninitialized constant remotetable

Any ideas please?

I have a requirement to observe a remote database table.

ActiveRecord observers do not observe tables, they observe objects. So when you update a user, it can observe that the user is updating and inject itself into the user lifecycle.

For you to act upon a foreign transaction that you do not have control over will require hooking into that database, using a trigger or pub/sub if available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM