Model to implement users (Ruby on Rails 3)
I am implementing a User that is not actually deleted from the system with destroy but only marked with 开发者_运维问答:active = false
.
The problem here is that such an inactivate user will show up in all User.find
, User.all
, ... calls. I don't want to pollute the code with all kinds of 'if-else's or overwriting the behavior of .find
, .all
etc.
I just want to know whether I can nicely define it within the User's model so that inactive users will virtually disappear unless I explicitly want to extract such a user.
If there is no way to do it in the model then what are my options?
Use a scope, or a class method with a where
clause.
I think you may want to check acts_as_paranoid
Here is a link for one of the implementations: https://github.com/technoweenie/acts_as_paranoid
From the wiki:
Now whenever destroy is called on that model, it is just removed from view and the deleted_at column set to the current date time. All the finder methods ignore “deleted” records.
精彩评论