Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 ActiveRecord: UNION

Is there any way to use MySQL UNION in Rails 3?

like image 711
krn Avatar asked Apr 07 '11 15:04

krn


1 Answers

I think the only way you're going to get this to work by directly executing the query.

ActiveRecord::Base.connection.execute("SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10)")

This returns an ActiveRecord resultset. If you want the results wrapped in a model do something like this:

MyModel.find_by_sql("...")
like image 124
Dan Fox Avatar answered Oct 22 '22 21:10

Dan Fox