Using greater than or less than in a Rails Active Record query

Don*_*n P 6 ruby-on-rails rails-activerecord

Questions have_many question_tags.

How can I get all questions that:

  1. Have a question_tag with name "javascript"
  2. Are not answered
  3. Have more than 2 "vote_count"?

Here are the tables:

Questions
  is_answered:boolean
  vote_count:integer

QuestionTags
  name:string
  question_id:integer
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止的查询.它做#1和#2.我怎么能做#3?

Question.joins(:question_tags).where(question_tags: {name: "javascript"}, question: {is_answered: false})
Run Code Online (Sandbox Code Playgroud)

Coe*_*ulf 5

这看起来像是这个问题的重复。您想要的是where的字符串或数组语法。

Question.joins(:question_tags).where(question_tags: {name: "javascript"}, is_answered: false).where(["#{Question.table_name}.vote_count > ?", 2])
Run Code Online (Sandbox Code Playgroud)

更新为在最后的where子句中包含表名。