3

i have 2 individual RAILS queries that both work fine on their own:

@charts = Chart.where(:patient_id => @emailpatient.id) @charts = Chart.where(:patient_id => @sharepatient.id) 

that i want to simply combine into a single 'OR' query. i tried:

@charts = Chart.where(:patient_id => @sharepatient.id OR :patient_id => @emailpatient.id) 

and

@charts = Chart.where("patient_id => @sharepatient.id OR patient_id => @emailpatient.id") 

and various other variations and cant get it to work. Where am i going astray?

4
  • You should get the queries in console where you ran ”rails server”. What does it say there? Commented May 21, 2013 at 7:37
  • Or rather, this is either syntax error (is uppercase OR available as comparison operator?) Commented May 21, 2013 at 7:38
  • @Smar the first line, he does not write the condition in a string => out. Then he does but does not interpolate both values. Commented May 21, 2013 at 7:41
  • 1
    @charts = Chart.where(:conditions => ["patient_id => ? OR patient_id => ?", @sharepatient.id, @emailpatient.id]) Commented May 21, 2013 at 7:45

2 Answers 2

8

You can do

@charts = Chart.where(:patient_id => [@emailpatient.id, @sharepatient.id]) 
Sign up to request clarification or add additional context in comments.

1 Comment

This translates to a WHERE patient_id IN (...) clause, which is logically equivalent but has the advantage of working for N ids.
5

You got it slightly wrong.

@charts = Chart.where("patient_id = ? OR patient_id = ?", @sharepatient.id, @emailpatient.id) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.