0
내 생각대로 사용자 모델에서 Rolify gem에 따라 사용자 역할을 추가하려고합니다.생성하기 전에 다른 모델을 저장하려고합니까?
기본적으로 달성하고자하는 것은 사용자가 등록 페이지 자체에서 학생 또는 교사임을 선택한 경우 사용자 생성 후 필요한 역할을 사용자에게 추가해야한다는 것입니다.
사용자 테이블에 '역할'을 저장하지 않습니다. 나는 attr_accessor를 사용하여 비교할 초기 값을 보내고있다.
이 내 사용자 모델 코드 :
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(255) default(""), not null
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# avatar :string(255)
# username :string(255)
#
class User < ActiveRecord::Base
# This is for the user roles.
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessor :role
# Adding the carrierwave uploader
mount_uploader :avatar, AvatarUploader
before_create :add_specified_role
def add_specified_role
if self.role == 0
# I am sure I am messing it up here and this is not the way.. :/
after_create :add_student_role
elsif self.role == 1
after_create :add_teacher_role
end
end
def add_student_role
self.add_role(:student) if self.roles.blank?
end
def add_teacher_role
self.add_role(:teacher) if self.roles.blank?
end
end
그러나, 내가 역할을 확인하는 경우와 같이 작동하지 않는 것, 역할이 추가되지 않았으며 내가 뭔가 잘못하고있는 중이 야 확신합니다.
위 작업을 수행하는 올바른 방법은 무엇입니까?
저장하고 있지 않다한다. 따라서 'role'이있는 열이 없으므로 after_create 콜백에 대해서는 존재하지 않으므로이 현재 시나리오에서 after_create가 작동하지 않습니다. Rolify gem의 모든 목적을 쓸데없이 렌더링 할 때 사용자 테이블에 다른 역할 열을 저장하고 싶지 않습니다. –
@SankalpSingha 사용자 테이블에 다른 역할을 저장한다는 것은 무엇을 의미합니까 ??? , Rolfiy 당신을 위해, 당신은 테이블의 필드가 아닌'attr_accessor : role'을 추가했습니다 –
@SankalpSingha rolify 다른 역할 테이블에 역할을 저장합니다. has_and는 사용자와 많은 관련이 있습니다 –