0

リモート データベース テーブルを観察する必要があります。次のようなコードを見ています。

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

私が追加しました

config.active_record.observers = :remotetable_observer

私のapplication.rb構成ファイルに、私のdatabase.ymlがリモートデータベースに接続します。

私は得ているNameError: uninitialized constant remotetableので、さらにモデルを作成しました:

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

それでも同じエラーが発生します:NameError: uninitialized constant remotetable

アイデアはありますか?

4

1 に答える 1

2

リモート データベース テーブルを観察する必要があります。

ActiveRecord オブザーバーはテーブルを観察するのではなく、オブジェクトを観察します。したがって、ユーザーを更新すると、ユーザーが更新されていることを監視し、ユーザーのライフサイクルに自分自身を挿入できます。

制御できない外部トランザクションに対処するには、トリガーまたは使用可能な場合は pub/sub を使用して、そのデータベースにフックする必要があります。

于 2013-09-30T14:59:40.503 に答える