SlideShare a Scribd company logo
Data Integrity in Rails
Strategies and techniques to make your data bulletproof
Proactive
Reactive
VS
Simple Complex
Email ValidationsNot NULL constraint
In ApplicationIn Database
Proactive Techniques
NOT NULL Constraint
The “NoMethodError”
@blog.body.to_html
Returns nil
undefined method ‘to_html'
for nil:NilClass
Fix in view
@blog.body.try(:to_html)
Fix in view
@blog.body.to_s.to_html
Fix in view
unless @blog.body.nil?

@blog.body.to_html

end
Learnings
Likely to encounter the same problem in other places
Messy code littered with checks and guards
These are all band-aid fixes
Fix using Rails Validation
validates :body,
presence: true,
if: ->(record) { record.nil? }
WTH?!?
Validations can
still be bypassed
blog = Blog.new

blog.body = nil

blog.save(validate: false)
Learnings
Code is unnecessarily hard to read
Validations can be bypassed, resulting in incoherent data
Fix in Database
change_column :blog, :body, :text,
null: false,
default: ''
Learnings
No Code Modification
Less Complexity–you never have to deal with both
nils and blank strings
Work on the assumption that body is never nil
The missing Parent
The “NoMethodError”
@post.author.name
Returns nil
undefined method ‘name'
for nil:NilClass
Deleting parent but not
children results in this error
Fix in view
@post.author.name if @post.author
Fix in view
@post.author.try(:name)
Learnings
Likely to encounter the same problem in other places
Messy code littered with checks and guards
These are all band-aid fixes
Fix using ActiveRecord
has_one :author,
dependent: :destroy
Fix using ActiveRecord
has_one :author,
dependent: :destroy
Inefficient if lots of records
Fix using ActiveRecord
has_one :author,
dependent: :delete_all
Does only one query,
but doesn’t run callbacks
Fix using ActiveRecord
has_one :author,
dependent: :restrict_with_exception
Blows up if you try to delete a
parent with children in DB
Fix using ActiveRecord
has_one :author,
dependent: :restrict_with_error
Shows an error if you try to delete
a parent with children in DB
These strategies can
still be bypassed
Post.find(1).delete
Learnings
This is better than fixing locally in views
But this can still introduce bad data
Fix in Database
add_foreign_key :authors, :posts
Fix in Database
add_foreign_key :authors, :posts
Rails 4.2 Feature
Fix in Database
ALTER TABLE `authors`

ADD CONSTRAINT `authors_post_id_fk`

FOREIGN KEY (`post_id`) REFERENCES `posts`(id);
Fix in Database
add_foreign_key :authors, :posts,

on_delete: :cascade
Removes all authors
when a post is deleted
Fix in Database
add_foreign_key :authors, :posts,

on_delete: :restrict
:restrict is the
default behavior of foreign keys
Ideal fix
has_one :author,
dependent: :delete_all
add_foreign_key :authors, :posts,

on_delete: :restrict
Learnings
The ideal fix never allows someone to directly introduce
orphan data, but still does the optimized cascading
behavior when deleted in ActiveRecord.
Duplicate Data
Uniqueness Validation
validates :name, uniqueness: true



Author.where(name: "Mr. Duplicate").count

# => 2

Uniqueness Validation
author = Author.new

author.name = "Mr. Duplicate"

author.save(validate: false)
Unique Index
add_index :authors, :name, unique: true
Unique Index
PG::Error: ERROR:
could not create unique index
"index_authors_on_name"

DETAIL: Key (name)=(Mr. Duplicate) is
duplicated.
Ways of Removing Duplicate
Data
Use SQL to arbitrarily remove duplicates
Use scripts to automatically merge content in rows
Manually merge content/remove duplicate rows
Unique Index Protects Data
from having Duplicates
PG::Error: ERROR:
duplicate key value violates unique constraint
"index_authors_on_name"

DETAIL: Key (title)=(Mr. Duplicate) already exists
This error is thrown every time the
Active Record validation is bypassed
Unique Index Protects Data
from having Duplicates
def save_with_retry_on_unique(*args)

retry_on_exception(ActiveRecord::RecordNotUnique) do

save(*args)

end

end
Retries saving when error is thrown,
so the validation can take over
Unique Index Protects Data
from having Duplicates
def save_with_retry_on_unique(*args)

retry_on_exception(ActiveRecord::RecordNotUnique) do

save(*args)

end

end Retries only once
Calls the block only once
One-to-One Relationships
add_index :authors, :name, unique: true
Protects from associating multiple
records to the parent
Learnings
Active Record validations are not meant for data integrity.
Incoherent Data can still be introduced.
Database level index on unique makes sure data is never
duplicated.
Rails will skip validations a lot in concurrent situations, so
always handle the underlying
ActiveRecord::RecordNotUnique Error.
Don’t forget to add unique index on one-to-one relationships.
Polymorphic
Associations
Polymorphic Association
class Post

has_many :comments, as: :commentable

end



class Comment

belongs_to :commentable, polymorphic: true

end
Both commentable_type and
commentable_id are stored in the database.
Polymorphic Association
class Post

has_many :comments, as: :commentable

end



class Comment

belongs_to :commentable, polymorphic: true

end
There is no way to add foreign keys
to polymorphic associations.
Learnings
There is no SQL standard way of adding polymorphic
associations.
Referential Integrity is compromised when we use this
ActiveRecord pattern.
Expensive to index.
The data distribution isn’t usually uniform.
Harder to JOIN in SQL.
Database-friendly
Polymorphic Associations
class Post

has_many :comments, class_name: 'PostComment'

end



class PostComment

include Commentable



belongs_to :post

end
Learnings
Adding one table for each child type maintains data integrity.
Foreign keys can be added.
Extract similar behaviors using modules in Ruby in the
application.
Create a non-table backed Ruby class for creating comments
Use class_name option to designate which class name to
use when retrieving records.
Learnings
Easier to grok and operate.
Harder to aggregate over all comments regardless of type.
More expensive to add another parent type.
Use specific tables if you care for data integrity.
If data integrity is a non-issue, use polymorphic
associations. Event logging or activity feeds are good
examples.
Reactive Techniques
Data Integrity Test Suite
MAX_ERRORS = 50



def test_posts_are_valid

errors = []

Post.find_each do |post|

next if post.valid?



errors << [post.id, post.errors.full_messages]



break if errors.size > MAX_ERRORS

end

assert_equal [], errors

end
Data Integrity Test Suite
def test_post_bodys_are_not_nil

assert_equal 0, Post.where(body: nil).count

end
Learnings
Proactive techniques work best
They’re not always feasible if you have bad data already
Reactive integrity checks are a good alternative
Run these regularly against production data to surface
errors up.
Avoid using complex constraints.
Recap
Not null constraints
Unique indexes
Foreign keys
Refactor Polymorphic association into separate tables
Reactive integrity checks
Thanks!
@rizwanreza

More Related Content

PPT
SQL Injection
PDF
Sql Injection Myths and Fallacies
PPTX
SQL Injection in action with PHP and MySQL
PPTX
Sql injection
PPT
Sql Injection Attacks Siddhesh
PPT
Sql injection
PPT
A Brief Introduction in SQL Injection
PPT
D:\Technical\Ppt\Sql Injection
SQL Injection
Sql Injection Myths and Fallacies
SQL Injection in action with PHP and MySQL
Sql injection
Sql Injection Attacks Siddhesh
Sql injection
A Brief Introduction in SQL Injection
D:\Technical\Ppt\Sql Injection

What's hot (20)

PPTX
Ppt on sql injection
PPTX
Sql injection
PPTX
Sql injection - security testing
PPT
Sql injection
PPT
Advanced SQL Injection
PPTX
PPTX
Sql injection
PDF
A Related Matter: Optimizing your webapp by using django-debug-toolbar, selec...
PPTX
Introduction to Core Java Programming
PPTX
seminar report on Sql injection
PPTX
Mvc by asp.net development company in india - part 2
PPTX
SQL Injections (Part 1)
PPT
Sql injection attack
PPTX
Web API with ASP.NET MVC by Software development company in india
PDF
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
PPTX
SQL Injections - A Powerpoint Presentation
PPT
Sql injection attacks
PPTX
Data Handning with Sqlite for Android
PDF
Understanding advanced blind sqli attack
PDF
Sql Injection - Vulnerability and Security
Ppt on sql injection
Sql injection
Sql injection - security testing
Sql injection
Advanced SQL Injection
Sql injection
A Related Matter: Optimizing your webapp by using django-debug-toolbar, selec...
Introduction to Core Java Programming
seminar report on Sql injection
Mvc by asp.net development company in india - part 2
SQL Injections (Part 1)
Sql injection attack
Web API with ASP.NET MVC by Software development company in india
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
SQL Injections - A Powerpoint Presentation
Sql injection attacks
Data Handning with Sqlite for Android
Understanding advanced blind sqli attack
Sql Injection - Vulnerability and Security
Ad

Viewers also liked (7)

PPTX
專題展系統開發
PPTX
PG Training
PPTX
嵌入式平台移植技巧概說
PDF
Enabling CD in Enterprises with Testing - Anand Bagmar
PPTX
The Dangers of Early Adoption #STC15
PDF
手機自動化測試和持續整合
PDF
UX, ethnography and possibilities: for Libraries, Museums and Archives
專題展系統開發
PG Training
嵌入式平台移植技巧概說
Enabling CD in Enterprises with Testing - Anand Bagmar
The Dangers of Early Adoption #STC15
手機自動化測試和持續整合
UX, ethnography and possibilities: for Libraries, Museums and Archives
Ad

Similar to Data Integrity in Rails (20)

PDF
Schema plus
PPTX
Day 4 - Models
PDF
Rails Tips and Best Practices
KEY
Rails Model Basics
PDF
Introduction to Active Record at MySQL Conference 2007
PPTX
Intro to Rails 4
ZIP
Rails and alternative ORMs
PDF
Introduction to Active Record - Silicon Valley Ruby Conference 2007
ODP
Ruby on rails
PDF
Using Database Constraints Wisely
PPT
Intro to Rails ActiveRecord
PDF
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
KEY
Rails and Legacy Databases - RailsConf 2009
PPTX
associations.pptx
PDF
Active domain
PDF
ActiveRecord Query Interface (2), Season 2
PDF
Ruby is Awesome
PDF
Rails data migrations
PDF
Effective ActiveRecord
ZIP
Barcamp Auckland Rails3 presentation
Schema plus
Day 4 - Models
Rails Tips and Best Practices
Rails Model Basics
Introduction to Active Record at MySQL Conference 2007
Intro to Rails 4
Rails and alternative ORMs
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Ruby on rails
Using Database Constraints Wisely
Intro to Rails ActiveRecord
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Rails and Legacy Databases - RailsConf 2009
associations.pptx
Active domain
ActiveRecord Query Interface (2), Season 2
Ruby is Awesome
Rails data migrations
Effective ActiveRecord
Barcamp Auckland Rails3 presentation

Recently uploaded (20)

PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
System and Network Administration Chapter 2
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Nekopoi APK 2025 free lastest update
PPTX
Introduction to Artificial Intelligence
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo POS Development Services by CandidRoot Solutions
System and Network Administration Chapter 2
Softaken Excel to vCard Converter Software.pdf
ai tools demonstartion for schools and inter college
Nekopoi APK 2025 free lastest update
Introduction to Artificial Intelligence
How to Migrate SBCGlobal Email to Yahoo Easily
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
CHAPTER 2 - PM Management and IT Context
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Operating system designcfffgfgggggggvggggggggg
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Upgrade and Innovation Strategies for SAP ERP Customers

Data Integrity in Rails