Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

This is the query I have so far. It does #1 and #2. How can I do #3?

Question.joins(:question_tags).where(question_tags: {name: "javascript"}, question: {is_answered: false})
like image 980
Don P Avatar asked Dec 30 '14 17:12

Don P


1 Answers

This looks like a duplicate of this question. What you want is the string or array syntax for where.

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

Updated to include the table name in the last where clause.

like image 200
Coenwulf Avatar answered Oct 17 '22 14:10

Coenwulf