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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:30:57+00:00 2026-06-18T23:30:57+00:00

I have an ActiveRecord model Account : class Account < ActiveRecord::Base attr_accessible :msisdn validates_uniqueness_of

  • 0

I have an ActiveRecord model Account :

class Account < ActiveRecord::Base

  attr_accessible :msisdn

  validates_uniqueness_of :msisdn, :on => :create, 
    :message => "User Already Registered ."
end

And I have a controller which try to create an account :

begin
  account = Account.create!(:msisdn => user)   
rescue Exception => e
  $LOG.error "Account #{user} : --> #{e.message}"  
end

Now the e.message always return : Validation failed: Msisdn User Already Registered, how am I supposed just to get just the message alone like User Already Registered. please note that I’m not using views at all, I want to use it from controller, and I’m using Rails 3.

Thanks in advance

  • 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-18T23:30:58+00:00Added an answer on June 18, 2026 at 11:30 pm

    When valid? is called on any model (which happens from create/save/update_attributes) it populates an errors object on the model. Of course if you use a bang method (create!) then the assignment will never happen, so use a non bang method instead. See 3rd code snippet.

    account = Account.new(:msisdn => user)
    unless account.save #
      # account.errors will be populated with errors
      puts account.errors[:msisdn] # => ['User Already Registered']
    end
    

    Alternative using a bang method

    account = Account.new(:msisdn => user)
    begin
      account.save!
    rescue Exception
      puts account.errors[:msisdn]
    end
    

    Edit:

    Another alternative after looking at the rails api docs is to get the record from the exception as it stores a copy. This makes my original statement false.

    ActiveRecord::RecordInvalid
    (github)

    begin
      account = Account.create!(:msisdn => user)
    rescue ActiveRecord::RecordInvalid => e
      puts e.record.errors[:msisdn] # => ['User Already Registered']
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an accounts model as follows (simplified): class Account < ActiveRecord::Base attr_accessible :account_number,
I have an Account model and a User model: class Account < ActiveRecord::Base has_many
I have a model as follows: class User < ActiveRecord::Base attr_accessible :name, :email, :twitter,
I have Account model, class Account < ActiveRecord::Base has_many :account_games def score account_games.map(&:score).sum end
I have a model, for example : class Account < ActiveRecord::Base before_create :build_dependencies def
I have three models, Account, User and Contact: class User < ActiveRecord::Base has_one :account
I have a standard ActiveRecord model with the following: class MyModel < ActiveRecord::Base custom_method
If I have an ActiveRecord::Base model with a default-scope: class Foo < ActiveRecord::Base default_scope
I have the following line in my ActiveRecord model: class Record < ActiveRecord::Base has_many
It seems simple, in my model I have: class CustomerAccount < ActiveRecord::Base acts_as_url :name

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.