ruby - Rails validates_length_of is misbehaving -


i have rails model adding validations , seem have run bit of weirdness 1 of validators.

so here table working (from schema.rb):

  create_table "clients", :force => true |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 normal view, with:

<%= error_messages_for 'client' %> <h1>new client</h1> <% form_for @client |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 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 part kicking butt first validation in model.

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

if head off browser , load view (/clients/new), enter client_id , name, select office, hit submit. validator not picking :client_id correctly, fail "too short" or "too long" error messages.

the kicker give me "too short" error until try 11 characters, @ 12 characters, "too long". 11 threshold of "too short", tho range supposed "5..7" - instead of "too long" message, validate , insert record, record inserts has different number "client_id", , it's same, despite validates_uniqueness_of.

what think happening :client_id, instead of validating actual field, client_id, it's trying pick objects id, , validate that. @ least, thing can think of.

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

above, server logs, validates "too short" :client_id

so please, there way correct weirdness? (note: tried validates_length_of "client_id", :in => 5..7 got absolutely no validation)

client_id column integer. validates_length_of uses size method find out length of field, , integer gives size of variable in bytes, 4 first 11 "characters" , 8 12+.

if need client_id integer , validate length, can use:

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

Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -