【发布时间】:2015-04-28 19:12:24
【问题描述】:
这是我的关联模型:
class ProjectLineThreshold < ActiveRecord::Base
belongs_to :project_line
belongs_to :source, class_name: 'Language'
belongs_to :target, class_name: 'Language'
end
ProjectLineThreshold 表有这些列(:id, :source_id, :target_id, :score)。我需要通过语言表中的 source_id 和 target_id 添加语言名称。
我想出了这个说法:
thresholds = self.project_line_thresholds.joins(:source, :target)
.select('project_line_thresholds.id, project_line_thresholds.source_id, project_line_thresholds.target_id,
project_line_thresholds.score, languages.name as source_name, languages.name as target_name')
但我的目标和源名称相同。什么是正确的连接语句,或者我做错了?
【问题讨论】:
标签: sql ruby-on-rails postgresql join