开发者

Rails single table inheritance/subclass find condition in parent

开发者 https://www.devze.com 2022-12-24 19:52 出处:网络
I have a table called Users (class User < ActiveRecord::Base) and a subclass/STI of it for Clients (class Client < User).

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 user开发者_如何学Gos regardless of type.

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

Any clue on how to get this condition to work?

Thanks!


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


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!!

0

精彩评论

暂无评论...
验证码 换一张
取 消