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 6126437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:22:26+00:00 2026-05-23T16:22:26+00:00

Hopefully someone can help me on this one, it’s absolutely driving me crazy! Basically,

  • 0

Hopefully someone can help me on this one, it’s absolutely driving me crazy!

Basically, I have created an Authentication system on my app, using Devise and CanCan, which works fine. The problem arises when I try to assign a Role to a User on signup, the Users Role does not save, the sign-up is successful, but the User has no assigned Role. In my Console the Users Role comes up as nil!

I’ve set up a HABTM association between Users and Roles, and tried various options. Rails Console returns User Role:nil even if I input the role as a String in the sign-up form.

I am new to Rails and programming in general, would really appreciate some assistance people! Many Thanks in

User Model:

class User < ActiveRecord::Base
has_and_belongs_to_many :roles



# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :username, :password, :password_confirmation, :remember_me, :role_ids

def role?(role)
  return !!self.roles.find_by_name(role.to_s.camelize)
end

end 

Snippet from New Registration View:

<fieldset> 
<legend>Sign Up</legend>

<div class="inputs">
<%= f.input :username, :required => true, :autofocus => true %>
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password %>
<%= f.input :password_confirmation, :label => "Confirm Password" %>
</div>

<div>
<%= f.input :roles, :required => true, :collection => Role.find(:all).collect {|c| [ c.name, c.id ]}, :label => "Role" %>
</div>

Params

Processing by Devise::RegistrationsController#create

as HTML Parameters: {“commit”=>”Sign
up”,
“authenticity_token”=>”1HCmfOVATkz/LbALboU+Z2Bg/lBQseVNB2NeAG7GPHc=”,
“utf8″=>”✓”,
“user”=>{“password_confirmation”=>”[FILTERED]”,
“username”=>”test”, “role_ids”=>[“2”],
“password”=>”[FILTERED]”,
“email”=>”test@test.com”}}

[1m[35mRole Load (0.1ms)[0m SELECT
“roles”.* FROM “roles” WHERE
“roles”.”id” = 2 LIMIT 1 [1m[36mSQL
(0.1ms)[0m [1mSELECT 1 FROM “users”
WHERE (“users”.”email” =
‘test@test.com’) LIMIT 1[0m
[1m[35mAREL (0.3ms)[0m INSERT INTO
“users” (“reset_password_token”,
“role”, “email”,
“remember_created_at”,
“current_sign_in_ip”,
“encrypted_password”, “updated_at”,
“created_at”, “sign_in_count”,
“username”, “last_sign_in_at”,
“reset_password_sent_at”,
“last_sign_in_ip”,
“current_sign_in_at”) VALUES (NULL,
NULL, ‘test@test.com’, NULL, NULL,
‘$2a$10$8J6F2NgkK6Tas0AVMiocUujimZ7K3XcYFSmWGeYUzmGDN55WVUHxO’,
‘2011-07-10 23:30:45.820893’,
‘2011-07-10 23:30:45.820893’, 0,
‘test’, NULL, NULL, NULL, NULL)
[1m[36mSQL (0.1ms)[0m [1mINSERT INTO
“roles_users” (“role_id”, “user_id”)
VALUES (2, 33)[0m [1m[35mAREL
(0.1ms)[0m UPDATE “users” SET
“current_sign_in_ip” = ‘127.0.0.1’,
“updated_at” = ‘2011-07-10
23:30:45.827053’, “sign_in_count” = 1,
“last_sign_in_at” = ‘2011-07-10
23:30:45.826761’, “last_sign_in_ip” =
‘127.0.0.1’, “current_sign_in_at” =
‘2011-07-10 23:30:45.826761’ WHERE
“users”.”id” = 33

Redirected to
http://localhost:3000/ Completed 302
Found in 163ms

  • 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-23T16:22:26+00:00Added an answer on May 23, 2026 at 4:22 pm

    After many investigations, this question raised another question but no real here.

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

Sidebar

Related Questions

Hopefully someone can help me out on this one. Consider the following use-cases: Add
Quite a simple one I think, but hopefully someone can help out. I have
hopefully someone can help me with this odd problem. I have a database which
Ok, this is bit of an obscure question, but hopefully someone can help me
This is a weird one, but hopefully someone can give me an idea here.
Hopefully someone can help me with this problem. I'm starting to work with Drupal
hopefully someone can help me out, I've been struggling for a couple days. Basically
Hopefully, someone here can give me some light. I have been researching this issue
This issue is something which has me stumped, so hopefully someone can help. I
I have another beginner's question that hopefully someone can help with. I'm trying to

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.