views:

24

answers:

1

How to use rails to do this?

mysql> SET @t1=1, @t2=2, @t3:=4;
mysql> SELECT @t1, @t2, @t3, @t4 := @t1+@t2+@t3;
+2  A: 

Use ActiveRecord::Base.connection:

>> ActiveRecord::Base.connection.execute("SET @t1=1, @t2=2, @t3:=4;")
 => nil 
>> ActiveRecord::Base.connection.select_one(
    "SELECT @t1, @t2, @t3, @t4 := @t1+@t2+@t3;")
 => {"@t1"=>"1", "@t2"=>"2", "@t4 := @t1+@t2+@t3"=>"7", "@t3"=>"4"} 

For more methods you can use on this class, see the documentation here.

You can also call the connection method on any ActiveRecord class you have defined. For instance, if you have a model called Post, you can use Post.connection.execute("sql").

wuputah