Be careful with the Active Record
When you accidentally pass an empty array to the
where method using
NOT IN, you probably will not get what you expected:
A check if two date or time ranges A and B overlap needs to cover a lot of cases:
- A partially overlaps B
- A surrounds B
- B surrounds A
- A occurs entirely after B
- B occurs entirely after A
When your model is using a callback like
before_validation to calculate an aggregated value from its children, it needs to skip those children that are `#marked_for_destruction?…
As you learned in "Test ActiveRecord callbacks" you should test the callback methods and its correct invocation in *two separate tests…
When you need to insert many records into the same table, a fast way to do it is to have a [single INSERT statement describing multiple rows](http://www.electrictoolbox.com/mysql-insert-multiple-re…
validates_uniqueness_of does not consider "username" and "USERNAME" to be a collision. This is usually not what you want.
Say you have a user model
``` class User < ActiveRecord::Ba…
In the following example the method
update_offices_people_count won't be called when
office_id changes, because it gets overwritten by the second line:
There is a bug in ActiveRecord 2.3.x that leads to changes in nested forms getting lost.
class Project < ActiveRecord::Base has_many :tasks accepts_nested_attributes_for :tasks
For reasons that completely escape me, MySQL 5.x limits UTF-8 strings to U+FFFF and smaller.
Arel is a library that was introduced in Rails 3 for use in constructing SQL queries. Every time you pass a hash to where, it goes through Arel eventually. Rails exposes this with a public API th…
If you care about performance, never use a query like
UPDATE users SET has_message = 1 WHERE users.id IN (SELECT user_id FROM messages)
MySQL does not optimize this and seems to scan the tempo…
An association defined with
has_many :through will return the same record multiple times if multiple join models for the same record exist. If that isn't what you want, you need to add the `:uniq…
The Edge Rider gem allows you to collect an array of values for one field of your ActiveRecord elements:
>> User.active.collect_column(:id, :conditio...
Rails 4.0 introduced a helpful new method for ActiveRecord queries:
where.not. It can make clunky queries easier to read.
I had a huge MySQL dump that took forever (as in: days) to import, while I actually just wanted to have the full database structure with some data to use on my development machine.
After trying se…
DECIMAL column is used when it is important to preserve exact precision. It takes two parameters, where precision is the total number of digits and scale the number of digit…
CONCAT('foo', 'bar', NULL) = NULL
the NULL always wins.
If you would rather treat NULL as an empty string, use [
TLDR: This card explains which threads and processes interact with each other when you run a Selenium test with Capybara. This will help you understand "impossible" behavior of your tests.