以下のように、has_many 内で belongs_to 関係を使用しようとしています。
つまり、client_id によって制約を受けながら、DownloadSchedule に属する一意のレポートが必要です。
class DownloadSchedule < ActiveRecord::Base
serialize :custom_data
belongs_to :client
has_many :report_column_schedule_links
has_many :reports, -> { uniq where("report_column_schedule_links.client_id = ?", self.client.id) }, :through => :report_column_schedule_links
end
スローされるエラーは
Mysql2::Error: Unknown column 'report_column_schedule_links.client_id' in 'where clause': SELECT `reports`.* FROM `reports` WHERE (report_column_schedule_links.client_id = 1)
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'report_column_schedule_links.client_id' in 'where clause': SELECT `reports`.* FROM `reports` WHERE (report_column_schedule_links.client_id = 1)
これは has_many で可能ですか、それともカスタム結合を作成する必要がありますか? Rails4を使用しています。
[更新] report_column_schedule_links の構造は以下のとおりです。
create_table :report_column_schedule_links do |t|
t.integer :report_id
t.integer :report_column_id
t.integer :client_id
t.integer :schedule_id
t.integer :download_schedule_id
t.timestamps
end
MySQL エラーがステートメントにあることに気付くでしょう。
SELECT `reports`.* FROM `reports` WHERE (report_column_schedule_links.client_id = 1)
このステートメントは、has_many で結合を実行していません。
ありがとう、ジャスティン