我有一个名为“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!
发布评论
评论(2)
我不知道属性'type'是如何填充的。但是,如果 type 是数据库列,您将能够通过适当的 SQL 表达式对其进行过滤:
或者您的意思是函数“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:
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
好吧,想通了!
该应用程序使用默认控制器方法的库(类似于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!!