简体   繁体   English

哪个是使用ActiveRecord停用用户帐户的最佳方法?

[英]Which is the best way to deactivate an user account with ActiveRecord?

I have a big problem and would like your help, with an option to disable a user account with multiple dependencies. 我有一个很大的问题,希望得到您的帮助,可以选择禁用具有多个依赖项的用户帐户。

I believe that there is no magic to it, but let's see what can be done: 我相信没有魔力,但让我们看看可以做些什么:

I have a model called User::Account with all dependencies (has_manies, has_ones...), but these dependencies are used throughout the system and everything works well only with the existence or absence of these records. 我有一个名为User::Account的模型,包含所有依赖项(has_manies,has_ones ...),但这些依赖项在整个系统中使用,一切都适用于这些记录的存在与否。 There is nothing to define when these dependencies are active or inactive. 当这些依赖项处于活动或非活动状态时,无需定义任何内容。

But I need that when the model User::Account is defined as inactive (through a column in DB for example), all these dependencies are also disabled, as if they didn't exists, so I don't have to change the entire operation of the system. 但我需要当模型User::Account被定义为非活动时(例如通过DB中的列),所有这些依赖项也被禁用,就像它们不存在一样,所以我不必更改整个系统的运作。 Or a less painful way to make it happen. 或者一种不那么痛苦的方式来实现它。

Suggestions? 建议?

Thanks 谢谢

Without knowing more specifics it's hard to give exact advice, but I'd suggest you're probably looking for a before_filter . 在不知道更多细节的情况下,很难给出确切的建议,但我建议你可能正在寻找一个before_filter

Something of this sort. 这种东西。

class ApplicationController

  private

  def user_must_be_active!
    if !@user.active?
      flash[:error] = "User Account is not active"
      redirect_to user_profile_path
    end
  end

end

Thereby in any controller that a user must be active for we can say 因此,在用户必须处于活动状态的任何控制器中,我们都可以说

class SomeController < ApplicationController
  before_filter :user_must_be_active!
end

Or perhaps there are just some actions we care about 或许我们只关心一些行动

class SomeOtherController < ApplicationController
  before_filter :user_must_be_active!, :only => :some_action
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM