Rails: Direct query to database

| 0 comments

database connection      Sometimes, ActiveRecord is not enough… or maybe, it would be, but I don’t know how!

So I discovered some usefull stuff: ActiveRecord::Base.connection

We can ask directly to our db trough an sql request like this

ActiveRecord::Base.connection.select_all <SQL STRING>

It does return an array of Hash.

Take care of what kind of request you want to do. If it is an update, or insert or delete, the connection method you’ll need will be execute

ActiveRecord::Base.connection.execute <SQL STRING>

Leave a Reply

Required fields are marked *.

*