Is there currently a way to do a raw SQL select query using ActiveRecord in Rails 4.0.0.beta1? I see ActiveRecord::Base.execute no longer exists. What's the correct way of going about this?
Here try this, select example.. :
query = "select ...."
results = ActiveRecord::Base.connection.execute(query)
Just to add my ten pence, a raw query using Model.connection.execute won't return an ActiveRecord model - it'll return a raw data set.
The following will return ActiveRecord models:
MyModel.find_by_sql(query)
edit: assuming of course that you're running a select.
In Rails 4 (perhaps previous versions as well), if you're going with a custom query for speed, you can add a :skip_logging
argument to avoid writing to the log:
query = "SELECT ..."
results = MyModel.connection.execute(query, :skip_logging)
(Note: If I'm reading the sources correctly, this might not hold true in PostgreSQL.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With