2014-05-24 25 views
1

好吧,我開始用has_many編寫這個想要的幫助:通過跨多個數據庫,仍然這樣做,但我發現了一些奇怪的東西,也許我錯過了一些東西。has_many:通過多個數據庫

所以我在做什麼:

的Rails 3.2

1 MSSQL數據庫表叫部署

1的PostgreSQL數據庫表名爲報告

與一個PostgreSQL數據庫加入所稱協會的表格

class Deployment < ActiveRecord::Base 
    self.primary_key = :id 
    establish_connection "deploy" 
    self.abstract_class = true 
    self.table_name = 'deployments' 

    has_many :associations 
    has_many :reports, through: :associations 

end 

class Report < ActiveRecord::Base 
    self.primary_key = :id 
    belongs_to :user 
    has_many :comments, dependent: :destroy 

    has_many :associations 
    has_many :deployments, through: :associations 

    accepts_nested_attributes_for :comments, allow_destroy: true 
    validates :weekending, presence: true, uniqueness: true 
end 

class Association < ActiveRecord::Base 
    attr_accessible :deployment_id, :report_id 
    belongs_to :deployment 
    belongs_to :report 
end 

協會的創建工作得很好,我得到兩個編號的表格中填入,但後來當我嘗試和獲取數據了,我得到以下幾點:

report = Report.find(16) 

report.deployments 

!! #<ActiveRecord::StatementInvalid: TinyTds::Error: Invalid object name 'associations'.: EXEC sp_executesql N'SELECT [deployments].* FROM [deployments] INNER JOIN [associations] ON [deployments].[id] = [associations].[deployment_id] WHERE [associations].[report_id] = 16'> 

,但是這就是我覺得奇怪,如果我這樣做的其他方式:

deployment = Deployment.find('0004d1bf-c49f-4310-85cd-222806d2eb78') 

deployment.reports 
[#<Report id: 15, weekending: "2019-01-17", visible: true, user_id: 5, news: "asdf", created_at: "2014-05-14 02:15:05", updated_at: "2014-05-14 02:15:05">] 

這是我所期望的,所以任何人都告訴我,爲什麼它不工作的其他方式?

+0

你是如何指示軌道的表在不同的rdbms上?因爲它認爲他們在那個失敗的查詢中在一起。 –

+0

@理查德,我只是使用establish_connection「部署」 – Phil

+0

我正在尋找一個名爲st-其他地方的寶石,但它已被觸及5年 – Phil

回答

0

由於它是一個具有amny一個錯誤我在你的代碼中看到的是report.deployment 應該

repost.deployments 

所以在模型

has_many deployments 
+0

抱歉,寫這個時只是一個錯字,也就是我現在看到的代碼 – Phil

+0

,我注意到您在查找中使用的ID不是標準ID,並且您的關係中沒有外鍵將其映射到新的ID,因此導軌試圖將'0004d1bf-c49f-4310-85cd-222806d2eb78'放入原點int列。告訴我,如果解決它。 – sonnyhe2002

+0

我已經改變了一些東西,並有一點進一步,但開始另一個問題 - [鏈接](http://stackoverflow.com/questions/23873852/rails-assication-not -saving-IDS) – Phil