Forum: Ruby on Rails SQL OR in RoR

C14bb449bccae95050e88bb254755120?d=identicon&s=25 Mike Oles (olesman)
on 2014-07-16 22:59
I am new to RoR and I am trying to convert one of my old PHP
applications into RoR.

However, I am not having any luck finding out how to do an OR statement
in RoR. My old statement in PHP is as follows...

SELECT * FROM games WHERE home_team = {param} or away_team = {param}
ORDER BY game_date ASC

I can do the OR in the model but I can't find a way to pass the param

I can pass the param in the controller but I can't find an OR statement
that will work.

I have also tried putting multiple statements together in the controller
but then I can't order them the way I want...

@home = Game.sorted.where(:home_team => params[:id])
@visitor = Game.sorted.where(:away_team => params[:id])
@games = @home + @visitors
#@games is what I refer to in the games.html.erb page but it sorts the
home games and then the visitor games so they aren't in order.

There has to be a way to do this. What am I missing?

34dbedb61632979ce94aaa9c6ae20c04?d=identicon&s=25 Eric Saupe (Guest)
on 2014-07-16 23:30
(Received via mailing list)
@games = Game.sorted.where("home_team = :id OR away_team = :id", id:

That should do the trick for you. You can have a string inside of your
where statement that equates to SQL. You can then have named parameters
that are used throughout the query as I have done above. In this case
are using the same variable for both but you could include more values
the hash at the end to be used in your query.
C14bb449bccae95050e88bb254755120?d=identicon&s=25 Mike Oles (olesman)
on 2014-07-17 00:06
Thanks Eric.

That did the trick.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.