Don P
Don P

Reputation: 63758

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})

Upvotes: 6

Views: 10145

Answers (1)

Coenwulf
Coenwulf

Reputation: 1937

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.

Upvotes: 5

Related Questions