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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:04:54+00:00 2026-05-14T14:04:54+00:00

I have a rails model that I am adding validations to and I seem

  • 0

I have a rails model that I am adding validations to and I seem to have run into a bit of weirdness from one of the validators.

So here is the table I am working with (From schema.rb):

  create_table "clients", :force => true do |t|
    t.string   "name"
    t.string   "last_contact"
    t.integer  "contacting_agent"
    t.date     "last_payment_date"
    t.float    "last_payment_amt"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "office"
    t.integer  "client_id"
  end

I have a normal view, with:

<%= error_messages_for 'client' %>
<h1>New Client</h1>
<% form_for @client do |new| %>

<table id='newform'>
 <tr>
  <th>Field</th>
  <th>Value</th>
 </tr>
 <tr>
  <td>
   ID
  </td>
  <td>
   <%= new.text_field :client_id %>
  </td>
 </tr>
 <tr>
  <td>
   Name
  </td>
  <td>
   <%= new.text_field :name %>
  </td>
 </tr>
 <tr>
  <td>
   Office
  </td> 
  <td>
   <%= new.select :office, $offices %>
  </td>
 </tr>
 <tfoot>
  <tr>
   <td>
    <%= image_submit_tag "/images/icons/save_32.png" %>
    <a href="/clients/new" title="Clear"><%= image_tag "/images/icons/close_32.png" %></a>
   </td>
   <td>
    &nbsp;
   </td>
  </tr>
 </tfoot>
</table>

<% end %>

and my humble model

class Client < ActiveRecord::Base
  validates_length_of :client_id, :in => 5..7
  validates_uniqueness_of :client_id
  validates_presence_of :name, :client_id
end

So the part that is kicking my butt is the first validation in the model.

validates_length_of :client_id, :in => 5..7

If I head off to the browser and load the view (/clients/new), I enter a client_id and a name, select an office, then hit submit. The validator is not picking up the :client_id correctly, as it will always fail with “too short” or “too long” error messages.

The kicker is that it will give me the “too short” error up until I try about 11 characters, then at 12 characters, I get the “too long”. So 11 is the threshold of “too short”, even tho the range is supposed to be “5..7” – but sometimes instead of the “too long” message, it will actually validate and insert the record, but the record it inserts has a completely different number for “client_id”, and it’s always the same, despite the validates_uniqueness_of.

What I think is happening is that :client_id, instead of validating the actual field, client_id, it’s trying to pick up the objects id, and validate that. At least, this is the only thing I can think of.

  Parameters: {"x"=>"13", "y"=>"14", "authenticity_token"=>"removed", "client"=>{"name"=>"test345", "client_id"=>"12345678", "office"=>"US10"}}

Above, from the server logs, validates as “too short” for :client_id

So please, is there any way to correct this weirdness?
(Note: I tried validates_length_of "client_id", :in => 5..7 but got absolutely no validation)

  • 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-14T14:04:55+00:00Added an answer on May 14, 2026 at 2:04 pm

    client_id column is integer. validates_length_of uses size method to find out length of the field, and for the integer it just gives you size of the variable in bytes, which is probably 4 for first 11 “characters” and 8 for 12+.

    If you really need client_id to be integer and validate length, you can probably use:

    validates_inclusion_of :client_id, :in => 10000..9999999, :message => "should be between 5 and 7 characters"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a rails model that validates uniqueness of 2 form values. If these
I have a polymorphic association in a Ruby on Rails model. In the migration,
I have a customer model that has_many events When I go to the customer
I have a model that has paperclip attachments. The model might be used in
I have a Rails app with some basic models. The website displays data retrieved
I have a rails application where each user has a separate database. (taking Joel
I have a rails application which is still showing the cachebusting numeric string at
I have a rails application running on a Linux server. I would like to
I have a rails form with a datetime_select field. When I try to submit

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.