Lee
Lee

Reputation: 8734

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

Upvotes: 0

Views: 1364

Answers (1)

Ven
Ven

Reputation: 19039

You should use before_create here.

Upvotes: 1

Related Questions