ActiveRecord::MySQL::Strict

Gem Version Code Climate Build Status

ActiveRecord::MySQL::Strict adds validations to ActiveRecord models to make sure they do not trigger errors in MySQL strict mode.

Installation

Add this line to your application's Gemfile:

gem 'activerecord_mysql_strict'

Usage

create_table "events" do |t|
  t.string   "name"
  t.string   "email", limit: 128
  t.text     "description"
  t.integer  "people_count"
end

class Event < ActiveRecord::Base
  validates_strict_columns
end

# String columns

event = Event.new(name: '.' * 400)
event.valid? # => false

event = Event.new(name: '.' * 255)
event.valid? # => true

event = Event.new(email: '.' * 200)
event.valid? # => false

event = Event.new(email: '.' * 100)
event.valid? # => true

# Text columns

event = Event.new(description: '.' * 70000)
event.valid? # => false

event = Event.new(description: '.' * 65535)
event.valid? # => true

# Integer columns

event = Event.new(people_count: 9999999999)
event.valid? # => false

event = Event.new(people_count: 2147483647)
event.valid? # => true

Options

You can use a few options when calling validates_strict_columns:

class Event < ActiveRecord::Base
  validates_strict_columns, only: [:name]
end

class Event < ActiveRecord::Base
  validates_strict_columns, except: [:people_count]
end

Todo

  • Support other MySQL column types that raise a Mysql2::Error exception when given a wrong value.

License

ActiveRecord::MySQL::Strict is © 2013 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for iPhone, iPad, Android, Blackberry, Windows Phone and Windows 8 in beautiful Quebec City.

We also love open-source software and we try to extract as much code as possible from our projects to give back to the community.