我有2個獨立的rails應用程序(應用程序a,應用程序b)。這兩個應用程序都維護一個客戶列表。我希望每天運行一次rake任務,並讓應用程序b從應用程序a中吸引選定的客戶。2 Rails應用程序之間的通信
這是我試圖解決這個問題的方法。如果我走錯了路,請告訴我。
我使用JBuilder中生成JSON
我的問題是如何讓應用B組中的應用程序A中的ID,讓系統知道客戶已經轉移了。
我假設我必須做一個類似於我所做的獲取客戶列表的放置請求,但是我遇到的問題得到了解決。
應用甲
顧客模型
scope :for_export, :conditions => {:verified => true, :new_system_id => nil ...}
顧客控制器
skip_before_filter :verify_authenticity_token, :only => [:update]
#
def index
@customers = Customer.for_export
end
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(params[:customer])
render :text => 'success', :status => 200
end
end
應用B
rake任務
task :import_customers => :environment do
c = Curl::Easy.new("http://domain.com/customers.json")
c.http_auth_types = :basic
c.username = 'username'
c.password = 'password'
c.perform
a = JSON.parse(c.body_str)
a.each do |customer|
customer = Customer.create(customer)
#put request back to server a to update field
end
end
末 結束
我所目前正在,我只是不知道這是正確的方法,以及如何發起PUT請求來調用客戶控制器的更新方法。
謝謝!
瑞安
我忘了提。無論你做什麼,都不要讓這兩個應用程序都可以編輯訪問同一個數據庫表。這是所有IT中最基本和最常被濫用的反模式之一。如果你這樣做,那麼你會後悔的。 –
謝謝你的建議湯姆。 我看到它的方式與在我的應用程序上使用API並且通過它與應用程序進行通信沒有什麼不同。我可以看到反模式,但是如果一個應用程序僅通過API進行通信,那麼它們實際上都不具有SQL訪問權限,並且我可以通過使用該API來控制情況。 – lundie