Rails validates_length_of is wrong

I have a rails model that I add validations to and I seem to be running into some weirdness from one of the validators.

So, here is the table I'm 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:

<%= 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 kicks my ass is the first check in the model.

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

      

If I go to the browser and load the view (/ clients / new), I enter the client_id and name, select the office, and then hit submit. The validator does not match correctly :client_id

, as it always fails with "too short" or "too long" messages.

The kicker is that it will give me an error that is too short until I try about 11 characters and then for 12 characters I get "too long". So 11 is a "too short" threshold even if the range should be "5..7", but sometimes instead of a "too long" message, it actually checks and inserts a record, but the insert record has a completely different meaning for "client_id". and it is always the same in spite of validates_uniqueness_of

.

I believe that :client_id

instead of checking the actual field, the client_id is trying to find the id of the objects and validate it. At least that's the only thing I can think of.

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

      

The above, from the server logs, is checked as "too short" for :client_id

So please, is there a way to fix this weirdness? (Note: I tried validates_length_of "client_id", :in => 5..7

but got absolutely no validation)

+2


a source to share


1 answer


Column

client_id

is an integer. validates_length_of

uses a method size

to determine the length of the field, and for an integer it just gives you the size of the variable in bytes, which is probably 4 for the first 11 "characters" and 8 for 12 +.

If you really need the client_id to be an integer and check the length, you can probably use:



validates_inclusion_of :client_id, :in => 10000..9999999, :message => "should be between 5 and 7 characters"

      

+1


a source







All Articles