Ruby on Rails - 在不同模式的模型中使用:include

发布于 2024-11-29 17:08:27 字数 665 浏览 0 评论 0原文

我正在使用 Ruby on Rails 模型。我有两个模型属于两个不同的模式。两个模型具有父子关系。例如,

class Group < ActiveRecord::Base
  has_one :customer
end

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

模型组位于 schema1 中,而客户位于 schema2 中。如果我使用以下代码执行此操作来加载组:

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

我收到错误 “schema1.Customer”是一个未定义的名称”,因为它试图在 schema1 而不是 schema2 中查找 Customer。

我如何更改此查询(或此:include)以指示客户位于 schema2 中。我尝试在 has_one 中添加 class_name组中的关系为 has_one :customer, class_name=>"Customer",但这并不能解决问题,我得到了同样的错误。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2024-12-06 17:08:27

你不能。您只能单独加载它们:

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

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)})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文