简体   繁体   中英

Rails before_save not working

I have a before-save action that is supposed to assign a user to a default role, but on creation a user is not assigned a role.

My code:

 before_save :assign_default_role, if: Proc.new { |user| user.role == nil }

  def assign_default_role
    self.role = Role.where(code: 'user').first
  end

您应在此处使用before_create

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM