Rails has_many :through 具有别名关联的条件

发布于 2024-12-22 19:29:02 字数 1590 浏览 0 评论 0原文

这种情况与我最初的问题有关has_many :through 有条件,但我发现了另一个问题。

鉴于此类:

class Contact < AR
  has_many :group_contacts
  has_many :groups, :through => :group_contacts, :conditions => {:published => true}
  has_many :anonymous_groups, :through => :group_contacts, :source => :group, :conditions => {:anonymous => true}
end

当我尝试将 anonymous_groups 包含在 contacts 中时,就会出现问题:

Contact.includes(:anonymous_groups)

ActiveRecord::StatementInvalid: PGError: ERROR: 缺少表“联系人”的 FROM 子句条目

其原因是生成的 sql 不正确。这类似于:

SELECT "group_contacts"."id" AS t0_r0 ... "groups"."anonymous" AS t1_r5 ... LEFT OUTER JOIN "groups" ON "groups"."id" = "group_contacts"."group_id" WHERE ("group_contacts".contact_id IN (...) AND ("contacts"."anonymous" = 'true'))

当然是转述的,但看看最终的条件。它将匿名条件放在 contacts 上,而不是 groups

现在,我们可以减轻这个查询错误(我确定这是一个错误),但执行以下操作

has_many :anonymous_groups, :through => :group_challenges, :source => :group, :conditions => { :groups => {:anonymous => :true} }

: sql 中正确的表上的条件,但是当我尝试构建匿名组时,我得到以下信息:

contact.anonymous_groups.build

ActiveRecord::UnknownAttributeError:未知属性:groups.anonymous

因此它适用于查询,但不适用于构建。我很确定这是一个错误,但我想知道是否有其他人经历过这个或有解决方法。

This kind of goes along with my original question about has_many :through with conditions, but I've found another problem.

Given this class:

class Contact < AR
  has_many :group_contacts
  has_many :groups, :through => :group_contacts, :conditions => {:published => true}
  has_many :anonymous_groups, :through => :group_contacts, :source => :group, :conditions => {:anonymous => true}
end

My problem happens when I try to include the anonymous_groups with contacts:

Contact.includes(:anonymous_groups)

ActiveRecord::StatementInvalid: PGError: ERROR: missing FROM-clause entry for table "contacts"

The reason for this is the generated sql is incorrect. It's something akin to:

SELECT "group_contacts"."id" AS t0_r0 ... "groups"."anonymous" AS t1_r5 ... LEFT OUTER JOIN "groups" ON "groups"."id" = "group_contacts"."group_id" WHERE ("group_contacts".contact_id IN (...) AND ("contacts"."anonymous" = 'true'))

Paraphrased of course, but look at the final condition. It's put the anonymous condition on contacts rather than groups

Now, we can alleviate this query error (which i'm sure is a bug) but doing something like:

has_many :anonymous_groups, :through => :group_challenges, :source => :group, :conditions => { :groups => {:anonymous => :true} }

This puts the condition on the correct table in sql, but when I try to build an anonymous group, I get this:

contact.anonymous_groups.build

ActiveRecord::UnknownAttributeError: unknown attribute: groups.anonymous

So it works for querying but not for building. I'm quite certain this is a bug, but I'm wondering if anyone else has experienced this or has a workaround.

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

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

发布评论

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

评论(1

无可置疑 2024-12-29 19:29:02

我认为类似 :conditions => “groups.anonymous = true” 应该可以正常工作。

I think something like :conditions => "groups.anonymous = true" should work fine.

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