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

  • Home
  • SEARCH
  • 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 8918289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:42:50+00:00 2026-06-15T05:42:50+00:00

I am using devise and cancan for user authentication and permissions. I need to

  • 0

I am using devise and cancan for user authentication and permissions. I need to create a default set of permissions for any NEW user ONLY. I don’t want to mess with devise code so it should be in my user model. What is the best way to do it.

Ok here is what I tried in my model:

before_create :setup_permissions

    def setup_permissions 
    permissions = []
        @roles = Role.where('subject_id is null and permissions = ?', 'Read')
            @roles.each do |role|
            permissions << {:role_id => role.id}
            end
        self.role_assignments_attributes = permissions  
    end

But the above code gives an error.

Interestingly I ran code to build permissions from above in console line by line. It produces correct array as shown below.

permissions =  [{:role_id=>15}, {:role_id=>17}, {:role_id=>19}, {:role_id=>21}, {:role_id=>23}, {:role_id=>25}, {:role_id=>27}]

And it works when I paste the permissions array directly in the model instead of building it as shown below:

def setup_permissions 
permissions =  [{:role_id=>15}, {:role_id=>17}, {:role_id=>19}, {:role_id=>21}, {:role_id=>23}, {:role_id=>25}, {:role_id=>27}]

    @roles = Role.where('subject_id is null and permissions = ?', 'Read')
        @roles.each do |role|
        permissions << {:role_id => role.id}
        end
    self.role_assignments_attributes = permissions  
   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-06-15T05:42:51+00:00Added an answer on June 15, 2026 at 5:42 am

    I got it to work by adding .to_a at the end of Model.where() to convert it into an array. Rails returns an ActiveRecord::Relationship when you use Model.where() clause instead of Array in case of Model.find(id). This should be corrected and made similar in both cases.

    before_create :setup_permissions
        def setup_permissions 
        permissions = []
            @roles = Role.where('subject_id is null and permissions = ?', 'Read').to_a
                @roles.each do |role|
                permissions << {:role_id => role.id}
                end         
           end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using devise for my authentication system and cancan for permissions. I am
I am using Devise and CanCan to manage users and user-rights. What I want
I'm quite new to this and I'm using cancan + devise for my user
I have been using devise for authentication and cancan for authorization in my application.
So I am using Devise for user authentication in my rails app. I have
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I have a rails app with authentication already set up using Devise. I'm adding
I'm using Devise and CanCan to create a backbone.js front-end and Rails 3.0.7 for
I have two entities. User and Role. I am using Devise and CanCan. They
While trying to add roles-based authentication (using CanCan and Devise) to my project I

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.