0

I have the following default scope defined in one of my models

default_scope order("IF(format = #{FORMATS[:wide]}, 1, 0) DESC, created_at DESC, name ASC")

It worked fine on my dev machine where I'm running MySQL, but borked when deployed to production where we use postgres. Is there a way to write that using Arel instead of straight SQL? FORMATS[:wide] returns an integer, but it may not be in any particular order. I just want records with that particular format to be returned first.

Chris Bloom
  • 3,526
  • 1
  • 33
  • 47

1 Answers1

0

I wouldn't put that in SQL at all. I would probably make a scope for each format type and then use Ruby to determine which to use. Or create a method that determines the sort order and passes that to wherever you are using it.

pcg79
  • 1,283
  • 9
  • 20
  • Thanks, that makes sense. I can always select each format separately and then join them in a single array in the order I need them. Seems like it would be more efficient to have the DB do what it's built to do though. – Chris Bloom Jul 22 '11 at 14:20