【问题标题】:I need to do a join where, but need to avoid repeating rows我需要在哪里加入,但需要避免重复行
【发布时间】:2011-02-14 20:40:39
【问题描述】:

我有一个范围

范围 :with_tags, lambda {|tag| joins(:tags).where('tags.name in (?)',tag )} #Questionnaire.with_tags(["Restaurants","Real State"])

Questionnaire.with_tags(["Restaurants","Real State"]).to_sql

=> "SELECT questionnaires.* FROM questionnaires INNER JOIN quest_tags ON questionnaires.id = quest_tags.questionnaire_id INNER JOIN tags ON tags@. = quest_tags.tag_id WHERE (tags.name in ('Restaurants','Real State'))"

我需要的是 "SELECT DISTINCT questionnaires.* FROM questionnaires INNER JOIN quest_tags ON questionnaires.id = quest_tags.questionnaire_id INNER JOIN tags ON @987654 id = quest_tags.tag_id WHERE (tags.name in ('Restaurants','Real State'))"

我需要对我的范围做些什么才能在选择中获得 DISTINCT

【问题讨论】:

    标签: ruby-on-rails-3 distinct where-clause


    【解决方案1】:

    范围 :with_tags, lambda {|tag| joins(:tags).where('tags.name in (?)',tag ).group('questionnaires.id')}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 2015-10-19
      • 2012-11-18
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多