views:

60

answers:

2

I find that the use of ActiveRecord affects the way I design the database schema (though I wish it wouldn't). I'm thinking about the inefficiency of fetching data and how to reduce the overall number of queries. The find :include option can only get you so far. I come from writing stored procs that grab everything you need (for a particular screen or activity) in a single call.

I really don't need an API for writing my SQL. I'm entirely content to write T-SQL in a non-Ruby way. The only thing I want is to have my query results mapped to instantiated models and their associations. Are there any ORMs that take this approach? Ones that can handle multiple selects (stored procs?) and maybe even the use of temp tables...

EDIT:

I rephrased and clarified what I was really after with this question.

+1  A: 

I find that the need for a quite complex query is about the 20% of a project, so using an ORM quite helps.

When that 20% arise, I find myself doing something alike what you ask, working excluvely with SQL. ActiveRecord and DataMapper have a find_by_sql method that helps you more, but doesn't instantiate all models (at least on ActiveRecord, if I'm not mistaken, that is).

Have you tried using Sequel? It's an ORM too but let's you have an easier approach and more flexibility on what you have.

Besides that, I can't think of a more focused solution on the ORMs realm. Keep in mind that the ORM tries to abstract the querying interface to simplify. If you are feeling confortable with using raw SQL, maybe you could be more productive with just an SQL facade interface.

Yaraher
I looked at Sequel. What I am unsure about is how to manually map associations in either ActiveRecord or Sequel. That is, if I just try to plug a Child (parent.children << child) into a model's association, ActiveRecord may confuse this as a new relationship rather than accepting it as an existing one.
Mario
A: 

look at the ActiveRecord find_by_sql() method.

http://github.com/rails/rails/blob/491f1b5f36a11427decc5d7f3558b5c3f5f243c1/activerecord/lib/active%5Frecord/base.rb#L660

Steven Soroka
I did know about find_by_sql, but thanks. I opened a new question. See the link above.
Mario