创建完成后,设置角色

发布于 2024-10-07 11:47:59 字数 1418 浏览 5 评论 0原文

我正在尝试使用 after_create 将默认用户角色设置为订阅者。但它似乎没有做出任何改变。新用户的角色始终是[" "]。

用户模型

class User < ActiveRecord::Base

  acts_as_authentic
  after_create :set_sub
  after_create :set_universal
  after_create :set_carrier

  def set_sub
    self.roles << "subscriber"
    #self.roles_mask = 4
  end

  def set_universal
      self.channels << Channel.find(1)
  end

  def set_carrier
    @carrier = Carrier.with_name(self.carrier_name)
    self.carrier<< @carrier
  end

  ROLES = %w[admin  moderator subscriber]

  #Each user can subscribe to many channels
  has_and_belongs_to_many :channels

  #Each user who is a moderator can moderate many channels
  #has_many :channel_mods
  has_and_belongs_to_many :modifies , :class_name => "Channel"

  #Each user can receive many messages
  has_and_belongs_to_many :messages

  #Each user belongs to a carrier
  belongs_to :carrier

  #Filter users by role(s)
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def role_symbols
  roles.map do |role|
    role.underscore.to_sym  # NOT role.name.underscore.to_sym (role is a string)
    end
  end


end

I'm trying to use an after_create to set the default user role to subscriber. But it doesn't appear to make any changes. The roles of the new user is always [" " ].

User Model

class User < ActiveRecord::Base

  acts_as_authentic
  after_create :set_sub
  after_create :set_universal
  after_create :set_carrier

  def set_sub
    self.roles << "subscriber"
    #self.roles_mask = 4
  end

  def set_universal
      self.channels << Channel.find(1)
  end

  def set_carrier
    @carrier = Carrier.with_name(self.carrier_name)
    self.carrier<< @carrier
  end

  ROLES = %w[admin  moderator subscriber]

  #Each user can subscribe to many channels
  has_and_belongs_to_many :channels

  #Each user who is a moderator can moderate many channels
  #has_many :channel_mods
  has_and_belongs_to_many :modifies , :class_name => "Channel"

  #Each user can receive many messages
  has_and_belongs_to_many :messages

  #Each user belongs to a carrier
  belongs_to :carrier

  #Filter users by role(s)
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def role_symbols
  roles.map do |role|
    role.underscore.to_sym  # NOT role.name.underscore.to_sym (role is a string)
    end
  end


end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2024-10-14 11:47:59

在 set_stub 方法中,您执行 self.roles << “订阅者”,这没什么作用。它修改角色返回的数组,但不会修改任何其他内容。

您需要调用 self.role = 并在保存之前执行此操作,以便它被保存。

def set_sub
  self.roles = [ "subscriber" ]
end

另一个 after_creates 工作的原因是因为它们工作在一个关系上,该关系定义了方法 << ,并且关系上的 << 会立即保存。

最好使用 before_validation 和/或 before_save 来完成这一切,并小心设置它,但不要保存它。您可以设置 self.channel_ids = [ 1 ],这不会立即触发保存,但会在您对模型调用 save 时保存。

In the method set_stub you do self.roles << "subscriber", which doesn't do much. It modifies the array returned by roles, but nothing else.

You need to call self.role = and do it before saving, so it gets saved.

def set_sub
  self.roles = [ "subscriber" ]
end

The reason the other after_creates work, is because they work on a relation, which has the method << defined, and << on a relation saves instantly.

It's probably better to do this all using before_validation and/or before_save and be careful to set it, but not to save it. You could set self.channel_ids = [ 1 ], which will not trigger a save instantly, but will get saved when you call save on the model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文