Hi,

I have a Users model that has a bunch of verifications such as email
being unique and password being present etc.. In my controller I want
to do @user.save and in case this failed due to the email not being
unique (The user being created is already present in the db), I want
to send an appropriate notice and redirect to a certain page. Save
only returns false when a certain validation fails, but I need to be
able to tell through code which validation it failed. I was expecting
it to throw certain types of exceptions based on what basic validation
failed but this is not how it behaves. Any ideas?

Thanks
Samir

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Michael Pavling at Nov 21, 2011 at 12:15 pm

    On 21 November 2011 05:28, Samir wrote:
    In my controller I want
    to do @user.save and in case this failed due to the email not being
    unique (The user being created is already present in the db), I want
    to send an appropriate notice and redirect to a certain page. Save
    only returns false when a certain validation fails, but I need to be
    able to tell through code which validation it failed. I was expecting
    it to throw certain types of exceptions based on what basic validation
    failed but this is not how it behaves. Any ideas?
    Have a look at the docs for ActiveRecord::Errors. If you call ".save!"
    it will raise an exception (if that's what you want), otherwise, you
    can inspect the errors object on an unsuccessful save to find out what
    went wrong.

    http://ar.rubyonrails.org/classes/ActiveRecord/Errors.html

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Sathia S at Nov 22, 2011 at 10:16 am



    I have a Users model that has a bunch of verifications such as email
    being unique and password being present etc.. In my controller I want
    to do @user.save and in case this failed due to the email not being
    unique (The user being created is already present in the db),
    In controller, You can get what error you get.
    Like,
    @user.errors
    @user.errors[:name]
    @user.errors.size // this will show how many errors.


    Regards
    sathia

    Here I share my experience in open source.
    http://www.sathia27.wordpress.com
    <http://www.sathia27.wordpress.com/>http://www.lquery.com<http://www.sathia27.wordpress.com/>

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 21, '11 at 9:56a
activeNov 22, '11 at 10:16a
posts3
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase