Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3500450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:44:07+00:00 2026-05-18T12:44:07+00:00

I’m trying to use an after_create to set the default user role to subscriber.

  • 0

I’m trying to use an after_create to set the default user role to subscriber. But it doesn’t appear to make any changes. The roles of the new user is always [” ” ].

User Model

class User < ActiveRecord::Base

  acts_as_authentic
  after_create :set_sub
  after_create :set_universal
  after_create :set_carrier

  def set_sub
    self.roles << "subscriber"
    #self.roles_mask = 4
  end

  def set_universal
      self.channels << Channel.find(1)
  end

  def set_carrier
    @carrier = Carrier.with_name(self.carrier_name)
    self.carrier<< @carrier
  end

  ROLES = %w[admin  moderator subscriber]

  #Each user can subscribe to many channels
  has_and_belongs_to_many :channels

  #Each user who is a moderator can moderate many channels
  #has_many :channel_mods
  has_and_belongs_to_many :modifies , :class_name => "Channel"

  #Each user can receive many messages
  has_and_belongs_to_many :messages

  #Each user belongs to a carrier
  belongs_to :carrier

  #Filter users by role(s)
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def role_symbols
  roles.map do |role|
    role.underscore.to_sym  # NOT role.name.underscore.to_sym (role is a string)
    end
  end


end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:44:07+00:00Added an answer on May 18, 2026 at 12:44 pm

    In the method set_stub you do self.roles << "subscriber", which doesn’t do much. It modifies the array returned by roles, but nothing else.

    You need to call self.role = and do it before saving, so it gets saved.

    def set_sub
      self.roles = [ "subscriber" ]
    end
    

    The reason the other after_creates work, is because they work on a relation, which has the method << defined, and << on a relation saves instantly.

    It’s probably better to do this all using before_validation and/or before_save and be careful to set it, but not to save it. You could set self.channel_ids = [ 1 ], which will not trigger a save instantly, but will get saved when you call save on the model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.