Search code examples
ruby-on-railsincludeschemamodels

Ruby on Rails - using :include in Model of different schema


I am working with Ruby on Rails models. I have two models belonging to two different schemas. Two models have parent child relationship. e.g

class Group < ActiveRecord::Base
  has_one :customer
end

class Customer < ActiveRecord::Base
  establish_connection "schema2"
end

Model Group is in schema1 and customer is in schema2. If I do this to load Groups using following code:

self.paginate(:all, :page => currentpage, :per_page => per_page, :include => :customer)

I get the error "schema1.Customer" is an undefined name" as it is trying to find Customer in schema1 instead of schema2.

How can I change this query (or this :include) to indicate that customer is in schema2. I tried to add class_name in has_one relationship in Group as has_one :customer, class_name=>"Customer", but it doesn't solve the problem, and I get the same error.

Any Ideas?


Solution

  • You can't. You can load them only separate:

    @groups = self.paginate(:all, :page => currentpage, :per_page => per_page)
    @customers = Customer.find(:all, :conditions => {:id => @groups.map(&:id)})