Rails and devise: model attribute not being persisted -
i edited devise's registrationscontroller::create
method modify behaviour. i'm trying if there no admin users in database, 1 first signs going assigned admin role, else regular user.
however, role, though assigned correctly object (tested), it's not being persisted database.
model:
class user < activerecord::base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessor :role roles = [ :admin, :default ] def is? requested_role self.role == requested_role.to_s end def self.admin_role return roles[0] end def self.default_role return roles[1] end end
modified devise method:
def create build_resource(sign_up_params) admin_user = user.find_by_role(user.admin_role) if admin_user.nil? resource.role = user.admin_role else resource.role = user.default_role end # here puts resource.role shows what's expected indeed being assigned object if resource.save ... end end
why isn't role
being stored in database? why null
?
you don't need attr_accessor
:role
if have defined column on table. activerecord
gives database backed accessors
having relevant column defined in relevant table.
your attr_accessor
overriding these , preventing them persisting changes database.
Comments
Post a Comment