1

I need to sort some resources with mysql. It requires of me to handle it like this

scope :sorted_by_source, -> { select("set_name(source) as source_name").order("source_name ASC") }

So as you can see function set_name is gonna return something which will later allow me to do order by. However i dont know how to define this function in Rails? Where should i put it, whats the syntax? (I'm gonna need switch statement)

Using Rails 3 and MySql

3
  • Why don't you just order by source? I just don't understand why you're trying to do this Commented Mar 20, 2014 at 14:37
  • its not in the database, trust me its kinda complicated and i really need this custom function Commented Mar 20, 2014 at 14:42
  • If it's not in the database you're going to need to sort it in ruby. Commented Mar 20, 2014 at 14:46

1 Answer 1

1

You can try adding migration and executing in up some sql to setup function

def up
  execute <<-SQL
   CREATE FUNCTION ...
  SQL
end

def down
  execute <<-SQL
    DROP FUNCTION ...
  SQL
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.