开发者

Redefining :all

I have users in my system that can elect to 'hibernate', at which point they can remove themselves and all of their associated records entirely from the system. I have queries all over my site that search within the User table and its associated tables (separated by as many as 5 intermediate tables), and none explicitly test whether the user is hibernating or not.

Is there a way to redefine the User set to non-hibernating users only开发者_高级运维, so all my current queries will work without being changed individually?

How can I most elegantly accomplish what I'm trying to do?


This is typically done with default scopes. Read all about them

Code from Ryan's site:

class User < ActiveRecord::Base
  default_scope :hibernate => false
end

# get all non-hibernating users
@users = User.all

# get all users, not just non-hibernating (break out of default scope)
@users = User.with_exclusive_scope { find(:all) }  #=> "SELECT * FROM `users`
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜