複数のデータベースにまたがって has_many :through を使用して、この必要なヘルプを書き始めました。
だから私がやっていること:
私は持っている
レール3.2
deployments と呼ばれる 1 つの MSSQL DB テーブル
reports と呼ばれる 1 つの POSTGRESQL DB テーブル
呼び出された関連付けを結合するための POSTGRESQL DB テーブルを使用
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
関連付けの作成は正常に機能し、テーブルに両方の ID が入力されますが、データを取得しようとすると、次のようになります。
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">]
これは私が期待することですが、他の方法で機能しない理由を誰か教えてください。