我有 users,roles,roles_users 三张表用来做角色的,在页面编辑 user 时 roles 字段是 select 形式保存 roles id 的,我想在保存 user 前对 role 进行转换然后保存,但非常奇怪的是触发不了 before_save 或者 before_update,更奇怪的是 logout 的时候 before_save 竟然被触发了,请求帮助!
class User < ActiveRecord::Base
before_save :setup_roles
before_update :setup_roles
def setup_roles
roles = roles.select{|r| r!=""}.collect{|r| Role.find(r)}
end
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :roles_users
has_many :roles, :through => :roles_users
end