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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:35:55+00:00 2026-05-26T11:35:55+00:00

I have a restrictive schema (i.e. the schema already has null restrictions and maximum

  • 0

I have a restrictive schema (i.e. the schema already has null restrictions and maximum length etc). Would putting all of them in the model also overkill and counterproductive?…

  validates :CouponID,      :presence => true,
                            :numericality => true

  validates :MerchantName,  :presence => true,
                            :length => { :maximum => 100 }

  validates :MerchantID,    :presence => true,
                            :numericality => true

  validates :Network,       :length => { :maximum => 20 }

  validates :Label,         :presence => true

  validates :CouponCode,    :length => { :maximum => 100 }

  validates :EndDate,       :presence => true

  validates :Link,          :presence => true

  validates :Status,        :presence => true,
                            :length => { :maximum => 45 }

  validates :Country,       :length => { :maximum => 100 }
  • 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-26T11:35:56+00:00Added an answer on May 26, 2026 at 11:35 am

    No it’s not an overkill. Putting these into validators your model would allow Rails to catch them before inserting them into the database. It’s also good design and practice.

    If you omitted these, you would get MySQL errors thrown instead.

    For example. Let’s say I have a Comment model which contains an attribute string called body which can not be nil in my table.

    class Comment < ActiveRecord::Base
    end
    

    If I tried:

    comment = Comment.create(body: nil)

    I would get the following exception.

    ActiveRecord::StatementInvalid: Mysql2::Error: Column 'body' cannot be null:

    This is bad. The natural flow of your application would break.

    But, if I put the validators in my model like so

    class Comment < ActiveRecord::Base
      validates :body, presence: true
    end
    

    and tried the following:

    comment = Comment.create(body: nil)

    I would not get an exception thrown but the errors array for my variable to tell me what went wrong.

    comment.errors.full_messages
    => ["Body can't be blank"]
    

    It’s good practice to put validators in your models and allows for good design.

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

Sidebar

Related Questions

I have run across an XML Schema with the following definition: <xs:simpleType name=ClassRankType> <xs:restriction
In C#, you can have more restrictive accessors on the accessors of a property
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
I have developed a web application (ASP.NET Web Forms). One of my customer has
I have a schema like below, the color elements of default and instance have
I use Hyperjaxb3 to generate JPA entities from XSD schema. I have a xsd:string
I have an XML schema that includes multiple addresses: <xs:element name=personal_address maxOccurs=1> <!-- address
We have a rather restrictive architecture where we have a portal-like Seam application which
I have problem with XML schema. I need inside one element elements of three
I would like to create XML Schema for this chunk of xml, I would

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.