如何使搜索逻辑命名范围返回 DISTINCT(不重复?)

发布于 2024-09-26 06:20:50 字数 182 浏览 1 评论 0原文

我有一个搜索逻辑,可以在可能多次出现的关联上搜索 not_null,但我只想显示该对象的一个​​唯一/不同实例:

Company.contact_emails_id_not_null

我只想要一个公司,无论有多少 contact_emails 与该公司关联:通过=> :联系人

I have a searchlogic that searches for not_null on an association that can occur many times, but I only want to display one UNIQUE/DISTINCT instance of the object:

Company.contact_emails_id_not_null

I only want one Company, no matter how many contact_emails are associated to that Company :through => :contacts

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

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

发布评论

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

评论(2

行雁书 2024-10-03 06:20:50

假设rails 3:

Company.contact_emails_id_not_null.select("distinct name_of_your_field")

如果rails 2.3.x(如果结果是假的,请原谅我不确定)

Company.contact_emails_id_not_null.find(:all, :select => "distinct name_of_your_field")

name_of_your_field 也可以是 * 以包含所有字段。

让我知道这是否有帮助。

Assuming rails 3:

Company.contact_emails_id_not_null.select("distinct name_of_your_field")

If rails 2.3.x (please forgive me if it turns out to be false I am unsure)

Company.contact_emails_id_not_null.find(:all, :select => "distinct name_of_your_field")

name_of_your_field can also be * to include all fields.

Let me know if that helps.

冰之心 2024-10-03 06:20:50

在 Rails 2.3.11 中,这对我有用...

@vendor_search = Vendor.searchlogic
@vendors = @vendor_search.paginate({
                                   :page => page,
                                   :per_page => 32,
                                   :order => 'name',
                                   :select => 'DISTINCT vendors.*'
                               })

其中有额外的项目特定信息,但相关部分是这样...

:select => 'DISTINCT vendors.*'

In Rails 2.3.11 this worked for me...

@vendor_search = Vendor.searchlogic
@vendors = @vendor_search.paginate({
                                   :page => page,
                                   :per_page => 32,
                                   :order => 'name',
                                   :select => 'DISTINCT vendors.*'
                               })

There's extra project-specific information in there, but the relevant part is this...

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