@games = Game.sorted.where("home_team = :id OR away_team = :id", id: params
[: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 you 
are using the same variable for both but you could include more values in 
the hash at the end to be used in your query.
 

On Wednesday, July 16, 2014 3:00:08 PM UTC-6, Ruby-Forum.com User wrote:
>
> 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 
> through. 
>
> 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? 
>
>
> Thanks 
> Mike 
>
> -- 
> Posted via http://www.ruby-forum.com/. 
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/e4e5d4ed-e2e1-403f-80e2-4681eb305813%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to