2013-09-30 19 views
0

我有一個要求觀察遠程數據庫表。我在看如下代碼:如何在rails上實現ruby遠程數據庫的Observer

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

任何想法嗎?

回答

2

I have a requirement to observe a remote database table.

ActiveRecord觀察者不觀察表,他們觀察對象。因此,當您更新用戶時,可以觀察到用戶正在更新並將自己注入用戶生命週期。

對於您無法控制的外部事務操作,需要使用觸發器或pub/sub(如果可用)掛接到該數據庫。

+0

所以我不能掛鉤到遠程rails數據庫/遠程rails環境和使用觀察者,因爲觀察者不能觀察遠程rails對象? – rupweb

+1

正確。您需要添加一個觀察者到遠程rails項目,該項目在THAT rails項目更新其對象時起作用。 –

相關問題