Rails单表继承/子类在父级中查找条件

发布于 2024-08-27 11:17:49 字数 468 浏览 6 评论 0 原文

我有一个名为“Users”(class User )的表和一个用于客户端的子类/STI(class Client )。

客户端“过滤”按预期工作,换句话说,Client.find(:all) 可以查找所有客户端。

但是,对于用户,我需要过滤结果以仅查找不是客户端的用户(其中​​类型为空或空白)。

我已在索引控制器中尝试了以下操作,但无论我为类型设置什么,它都会返回所有用户,无论类型如何。

User.find(:all, :conditions => { :type => nil }, :order => 'name')

关于如何让这种情况发挥作用的任何线索?

谢谢!

I have a table called Users (class User < ActiveRecord::Base) and a subclass/STI of it for Clients (class Client < User).

Client "filtering" works as expected, in other words Client.find(:all) works to find all the clients.

However, for users I need to filter the result to only find users that are NOT clients (where type is null or blank).

I've tried the following in my index controller but no matter what I put for the type it returns all users regardless of type.

User.find(:all, :conditions => { :type => nil }, :order => 'name')

Any clue on how to get this condition to work?

Thanks!

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

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

发布评论

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

评论(2

随波逐流 2024-09-03 11:17:49

我不知道属性'type'是如何填充的。但是,如果 type 是数据库列,您将能够通过适当的 SQL 表达式对其进行过滤:

User.find(:all, :conditions => [ " type=null or type='' "])

或者您的意思是函数“class”?据我所知,“type”已被弃用。
IRB 声明:警告:Object#type 已弃用;使用对象#类

I don't know how the attribute 'type' is filled. But if type is a database column you'll be able to filter it by an appropriate SQL expression:

User.find(:all, :conditions => [ " type=null or type='' "])

Or do you mean the function "class"? "type" is deprecated as far as I know.
IRB states: warning: Object#type is deprecated; use Object#class

活泼老夫 2024-09-03 11:17:49

好吧,想通了!

该应用程序使用默认控制器方法的库(类似于resource_controller),并且我错误地覆盖了用户控制器中的索引方法。

感谢阿希姆的帮助!

Ok, figured it out!

The app uses a lib for the default controller methods (similar to resource_controller) and I was incorrectly overwriting the index method in the user controller.

Thanks for the help Achim!!

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