如何使用ActiveRecord多次连接相同的表(ruby on rails)

时间:2016-07-12 11:48:41

标签: sql ruby-on-rails ruby

我通过阅读句子和翻译这些句子来学习单词的应用。

每个单词都有并且属于许多句子,每个句子都有翻译的句子('链接'表格)但不是所有特定语言的翻译(许多英语句子被翻译成日语但没有翻译成俄语)

我需要得到的单词(例如英语)有句子(英语)已翻译句子(俄语)

DB:

       words                 sentences_words            sentences              links(sentences_sentences)
_______________________    _____________________    __________________     _____________________________
|id |lang   |word     |    |word_id|sentence_id|    |id|lang|sentence|     |sentence_1_id|sentence_2_id|   
|1  |rus    |Ё        |    |   1   |     1     |    |1 |rus |  ЁЖ    |     |      1      |      5      | 
|2  |rus    |Ж        |    |   1   |     4     |    |2 |rus |  ЗЖ    |     |      1      |      8      | 
|3  |rus    |З        |    |   2   |     1     |    |3 |rus |  ЙЫ    |     |      2      |      6      | 
|4  |rus    |Й        |    |   2   |     2     |    |4 |rus |  ЁЗ    |     |      3      |      7      | 
|5  |rus    |Ы        |    |   3   |     2     |    |5 |eng |  ab    |     |      3      |      10     | 
|6  |eng    |a        |    |   3   |     4     |    |6 |eng |  bc    |     |             |             | 
|7  |eng    |b        |    |   4   |     3     |    |7 |eng |  ca    |     |             |             | 
|8  |eng    |c        |    |   5   |     3     |    |8 |jpn |        |     |             |             | 
|9  |jpn    | ...     |    |   6   |     5     |    |9 |jpn |        |     |             |             | 
|10 |jpn    | ...     |    |   6   |     7     |    |10|jpn |        |     |             |             | 
|   |       |         |    |   7   |     5     |    |11|jpn |        |     |             |             | 
|   |       |         |    |   7   |     6     |    |12|jpn |        |     |             |             | 
|   |       |         |    |   8   |     6     |    |13|jpn |        |     |             |             | 
|   |       |         |    |   8   |     7     |    |14|jpn |        |     |             |             | 

型号:

class Word < ApplicationRecord
  has_and_belongs_to_many :sentences
end

class Sentence < ApplicationRecord
  has_and_belongs_to_many :words
  has_and_belongs_to_many :translations,
                          class_name: "Sentence",
                          join_table: "links",
                          foreign_key: "sentence_1_id",
                          association_foreign_key: "sentence_2_id"
end

这个sql很好但我需要activerecord查询:

sql = "
    select w.word from words w
    join sentences_words sw on sw.word_id = w.id
    join sentences s1 on sw.sentence_id = s1.id
    join links l on l.sentence_1_id = s1.id
    join sentences s2 on l.sentence_2_id = s2.id
    where w.language = 'eng'
    and s1.language = 'eng'
    and s2.language = 'rus'
    group by w.id
    order by w.id"

@words = ActiveRecord::Base.connection.execute(sql)

UPD:

此代码也有效:

@words = Word.joins("INNER JOIN sentences_words sw ON sw.word_id = words.id
                     INNER JOIN sentences s1 on sw.sentence_id = s1.id
                     INNER JOIN links l on l.sentence_1_id = s1.id
                     INNER JOIN sentences s2 on l.sentence_2_id = s2.id
                     WHERE words.language = 'eng'
                     AND s1.language = 'eng'
                     AND s2.language = 'rus'").group(:id).order(:id)

有可能做那样的事吗? (它不起作用):

@words = Word.where(Sentence.where(language: 'eng').joins(:sentences).
where(sentences: {language: 'rus'})).where(language: 'eng')

@words = Word.joins(:sentences).joins(:translations).
where(words: {language: 'eng'}, sentences: {language: 'eng'}, translations: {language: 'rus'}

谢谢!

1 个答案:

答案 0 :(得分:0)

你好@Dmitry你可以使用嵌套连接。这是[http://guides.rubyonrails.org/active_record_querying.html#joins]

的良好链接