How do I perform this ActiveRecord query?

Can you prevent developers from using raw SQL queries in ActiveRecord?

  • We are running a rails+mysql application. In my specific requirement I want all my developers to access mysql only throught the rails API. I want to disable all direct mysql queries outside rails.  I need this because I have a lot of callbacks running at the AR layer. How do I implement this? EDIT 1: I should be more clear. I don't care about someone directly logging into mysql and doing updates. But one should not be able to run a mysql query inside rails using the connection object directly. Is there a way to restrict that? EDIT 2: Our actual problem is like the below, We have a separate read only tables for performance. All the writes to master tables will create callbacks that update the readonly tables. The readonly tables are optimised for read performance. Now when a developer updates the master tables directly through SQL, the activerecord callbacks don't know about it and the readonly table data become inconsistent. This is our problem.

  • Answer:

    It sounds like you want to selectively block writing to the database? One way would be to override ActiveRecord::Base#readonly? to check if the active connection is on the master database or not. Alternatively, you could add an `after_find` find callback that blocks updates: def after_find readonly! if self.connection.readonly? end You'll have to add your own readonly? method to the connection object to match your specific conditions.

Brook Riggio at Quora Visit the source

Was this solution helpful to you?

Other answers

Just ask your developers not to write and run SQL queries directly on the connection object, and to use ActiveRecord::FinderMethods and ActiveRecord::QueryMethods (etc) APIs instead. But it's worth noting though that for some complex queries you're probably going to get better performance writing and running SQL directly. It's not practical to enforce this at the API level, but you can perform code reviews and require fixes on code that needlessly includes raw SQL.

James Stradling

Anyone writing code within the Rails application is able to write raw SQL queries. If you have specific callbacks being run then a good series of tests both unit and integration should pick up any inconsistencies with this. Otherwise speak to the development team and let them know your concerns, GitHub pull requests are a good way to do code reviews, which is always a good development practice.

David White

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.