Leo
Leo

Reputation: 2103

How to define custom mysql function in Rails

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

Upvotes: 1

Views: 689

Answers (1)

Konstantin Ilchenko
Konstantin Ilchenko

Reputation: 485

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

Upvotes: 1

Related Questions