# Rails and SQL

**URL:** <https://www.ruby-forum.com/t/rails-and-sql/126596>\
**Category:** Rails\
**Created:** [January 20, 2008, 2:40am UTC](https://www.ruby-forum.com/t/rails-and-sql/126596 "2008-01-20T02:40:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 20, 2008, 2:40am UTC](https://www.ruby-forum.com/t/rails-and-sql/126596/1 "2008-01-20T02:40:22Z")

</div>

I work at a company that mainly does Java and PHP, as well as some  
old TCL. Alot of the code has these really big complex SQL queries  
where there is a select off of several tables and there is some nested  
inner select. It has occured to me in my advocacy of Rails that if I  
ever convinced them to take Rails more seriously and look into it that  
they might have some problem with the approach where in Rails you  
often don’t write those kinds of queries. Has anyone encountered that  
kind of criticism ?

I am not really much of an SQL expert myself, though at times I have  
had to work with some of these queries, I’ve only been at this company  
for a year. I know you can do straight SQL in Ruby, but typically in  
Rails with active record, you wouldn’t do these kinds of complex  
queries. I feel quite content myself to steer away from that kind of  
thing if I can, but I’m not sure how to make a case of it with people  
who might have a problem with it. I also get skeptical sometimes if I  
can ever convince anyone of anything, so sometimes I never really  
worry how to present a really complete argument anyway …

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 20, 2008, 4:13am UTC](https://www.ruby-forum.com/t/rails-and-sql/126596/2 "2008-01-20T04:13:19Z")

</div>

This is a valid concern on behalf of management, but really it’s just  
making them look at things differently. Most of my apps end up with  
everything coded AR style to begin with, except in the case of AR just  
not being able to create the object that I want. (Yes it does happen I  
have a 30 line sql statement in one app, that I don’t even want to  
consider trying to write with AR). Mostly I find myself refactoring  
some of Rails ORM layer into sql in order to remove bottlenecks, but  
this is towards the end of a project. The thing is ORM has saved me so  
much time during development that I can afford to spend a day or two  
re-factoring some of the complex finds or strange inserts that AR  
might do into straight up sql.  
At the end of the day all that Rails is doing is implementing an ORM  
layer (Object Relational Mapping) just like Java does with Hibernate  
and even PHP does with CakePHP framework. Personally I’d just look at  
selling ORM to them if that is their concern with rails.  
There is a great screen cast from the Rails envy guys about Active  
Record ( not ActiveRecord, note the space ), it might be worth looking  
at before tackling management.  
[http://www.railsenvy.com/2007/8/8/activerecord-tutorial](http://www.railsenvy.com/2007/8/8/activerecord-tutorial)

Good luck,  
Cam

On Jan 20, 12:29 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 20, 2008, 4:44am UTC](https://www.ruby-forum.com/t/rails-and-sql/126596/3 "2008-01-20T04:44:55Z")

</div>

On Jan 19, 10:12 pm, cammo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> might do into straight up sql.

Thanks,

I have the active record book that came out. I have almost all of the  
major Rails and Ruby books in existence. I’ve done a bunch of fairly  
small websites on my own in Rails. I think I sort of realize myself  
why I like AR, I just am not that great of a salesman in some way  
perhaps.
