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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:38:52+00:00 2026-05-30T16:38:52+00:00

UPDATED I’m using Devise 1.4.9 for authentication and my Devise-generated User model doesn’t seem

  • 0

UPDATED I’m using Devise 1.4.9 for authentication and my Devise-generated User model doesn’t seem to be catching the exceptions being thrown by the DB when I try to create a new user with an email address that already exists in the DB. After following cicloon’s suggestion (see response below), my code to create a new user is…

class Api::RegistrationsController < Api::BaseController

  respond_to :json

  def create
    user = User.new(params[:user])
    user.ensure_authentication_token! 

    if user.valid?
        user.save
        render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email, :user_id=>user.id), :status=>201
        return
    else
        warden.custom_failure!
        render :json=> user.errors, :status=>422
    end
  end
end

The Devise-generated migration includes an index for the email attribute, including uniqueness validation…

add_index :users, :email, :unique => true

And here’s the user model…

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :authentication_token 

  validates_uniqueness_of :email

  def ensure_authentication_token!   
    reset_authentication_token! if authentication_token.blank?   
  end  

  def as_json(options={})
    super(:only => [:email, :authentication_token, :id])
  end

end

…I would like the database to raise an error during save if the specified email address already existed in the DB, have Rails catch it and execute the else block of code to return a 422 and description of the problem. It doesn’t, instead I get an SQLException error and a crash…

ActiveRecord::StatementInvalid (SQLite3::ConstraintException: constraint failed: INSERT INTO "users" ("authentication_token", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)):
  app/models/user.rb:33:in `ensure_authentication_token!'
  app/controllers/api/registrations_controller.rb:7:in `create'

How can I get my code to return a 422 with a description of the error?

Thanks so much in advance for your wisdom!

  • 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-30T16:38:53+00:00Added an answer on May 30, 2026 at 4:38 pm

    I read your post again, now I see what’s happening there. You have a database index, and Rails is not aware of that, so when you make @user.save the database throws an exception which is not captured Rails side.

    That said, you have two choices:

    1.) Capture the exception in Rails:

    begin
      user.save
      render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email, :user_id=>user.id), :status=>201
    
    rescue
      warden.custom_failure!
      render :json=> user.errors, :status=>422
    end
    

    2.) Add the validation to your model and check for user.valid?:

    On your user model:

    validates_uniqueness_of :email
    

    On your controller:

      if user.valid?
          user.save
          render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email, :user_id=>user.id), :status=>201
          return
      else
          warden.custom_failure!
          render :json=> user.errors, :status=>422
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Updated a little ) I'm not very experienced with internationalization using PHP, it
UPDATED Using RedDot CMS, linking to a 'sub-page' (page within a page) directly will
[Updated a bit] I have a Task model that has a :completed boolean attribute.
[Updated below] I am having a problem connecting to a MySQL database using a
UPDATED: 06.29.10 Here's the code I'm using so far. I'm really close after searching
UPDATED 2010-11-25 A legacy stand-alone application (A1) is being re-created as a web application
UPDATED: to include model relationships I have a many to many join table between
UPDATED: Added one more question (Question #4). Hi all, I'm building myself a custom
UPDATED QUESTION Since the ctor is not supported by .NETCF (public FileStream(IntPtr handle, FileAccess
Updated question, see below I'm starting a new project and I would like 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.