Ruby on Rails Tuesday, May 6, 2014



On Monday, May 5, 2014 3:08:17 PM UTC-4, Tyler DeWitt wrote:
I've found there are some times where jumping straight to the database is easier than going through ActiveRecord (for instance: odd calculations being performed or large sets of data being returned that don't need to be instantiated into AR models).  I can't find an easy or accepted way to bind variables in a custom SQL clause.  sanitize_sql_array is protected, so you would have to call it with send(), which just feels dirty.  ActiveRecord::Base.connection.quote() doesn't play well with dates.

Am I missing a function that let's me use placeholders like the ActiveRecord where() with my plain SQL clauses?

If not, has this been a specific design to make sanitizing SQL fragments difficult? Perhaps to discourage it? Or is it due to the different database drivers?

Rails is usually straight forward and intuitive, I've just found with SQL fragments I have to pull teeth to kinda get it to work.

Jumping straight to the database is discouraged in Rails and it's something I've only rarely done or seen a need to do.  The main reason is that one of the goals of ActiveRecord is to make your application database agnostic.  You can use your application with MySQL, PostgreSQL, etc. and not have to worry about knowing the specific ins and outs of each database, it just works.  I would argue that has gotten a little muddy as 4.1 has a few features that are specific to PostgreSQL.  At any rate, writing SQL fragments is discouraged because you potentially now make things specific to one database management system (or even worse, a specific version of a database management system) rather than relying on ActiveRecord to handle the specifics.  You've started to stray from the conventions and, therefore, start to lose the efficiencies and make it more difficult to support the application (particularly if another developer inherits the application).  

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/64d86ac1-3777-4d12-bbd6-9f6e719d81ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment