0

[updating question to clarify]

Can I create a model in Rails 3/3.1 and make ActiveRecord automatically use/append some joins I configured to that model?

Ex:

Use code like this:

  class Component < ActiveRecord::Base
    def self.base_query
        joins("join t05 on d04.t05_ukey = t05.ukey left join d03 on d04.d03_ukey = d03.ukey left join d16 on d04.d16_ukey = d16.ukey")
    end
  end

  Component.first      # under the hood is doing Component.base_query.first
  Component.where(...) # under the hood is doing Component.base_query.where

But ActiveRecord calls the method base_query under the hood without the need to explicitly call it. Just to make it more Rails-like.

Any ideas?

Celso Dantas
  • 1,181
  • 10
  • 14

1 Answers1

0

Take a look at this guide about active record querying with Arel. There are a couple of really good railscasts (@railscasts.com) on the topic as well. And if you find yourself with ugly where clauses, etc, take a peek at the meta_where gem. I swear by these three things.

jaydel
  • 14,389
  • 14
  • 62
  • 98
  • Found that I can create a method (ex: basic_query) and includes all those joins(...) but I have to call "Component.basic_query" everytime I have to query a Component. I want ActiveRecord to append all those joins (from the sql query I showed) everytime I use the Component model, even if I call Component.find(1). – Celso Dantas Jul 11 '11 at 03:31