Rails SQL Join two tables, One table has two columns with ids of other table, I need to get names of these ids -
this model associations:
class projectlinethreshold < activerecord::base belongs_to :project_line belongs_to :source, class_name: 'language' belongs_to :target, class_name: 'language' end projectlinethreshold table has these columns (:id, :source_id, :target_id, :score). need add names of languages source_id , target_id languages table.
i came statement :
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 source_name, languages.name target_name') but same names target , source. proper join statement, or doing wrong?
the following query hit db once:
self.project_line_thresholds .joins(:source, :target) .includes(:source, :target) .map {|plt| [plt.id, plt.source_id, plt.target_id, plt.score, source.name, target.name]}
Comments
Post a Comment