创建完成后,设置角色
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 set_stub 方法中,您执行 self.roles << “订阅者”,这没什么作用。它修改角色返回的数组,但不会修改任何其他内容。
您需要调用
self.role =
并在保存之前执行此操作,以便它被保存。另一个 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.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/orbefore_save
and be careful to set it, but not to save it. You could setself.channel_ids = [ 1 ]
, which will not trigger a save instantly, but will get saved when you callsave
on the model.