# Given the following models class Image < ActiveRecord::Base has_many :album_images has_many :albums, through: :album_images
You know that ActiveRecord caches associations so they are not loaded twice for the same object. You also know that...
Instead of this: Image.order('images.created_at DESC') You can write this: Image.order(created_at: :desc) Not only do you not...
Web applications can be used by multiple users at the same time. A typical application server like Passenger has multiple...
RSpec 3 has verifying doubles. This breed of mock objects check that any methods being stubbed are present on an...
When running migrations with rake db:migrate, there's the STEP and VERSION parameters that you can pass to nearly...
tldr; Use git diff -M or git diff --find-renames when you've moved a few files around. Usage
If you want to load an SQL dump from an ActiveRecord migration, you might find this to be harder than...
Sequel is an awesome ORM such as ActiveRecord. The linked article describes how easily you can implement and use materialized...
Rails migrations allow you to use a change method whose calls are automatically inverted for the down path. However, if...
Creating records in specs can be so fast that two records created instantly after one another might have the same...
Returning an empty scope can come in handy, e.g. as a default object. In Rails 4 you can achieve this...
p ActiveRecord::Base.connection.indexes(:table_name)
This may be hard to find in the docs, but if you want CoffeeScript classes that instantiate their properties from...
class Document < ActiveRecord::Base scope :any_tags, -> (tags){ where('tags && ARRAY[?]', tags) } scope :all_tags, -> (tags){ where('tags @> ARRAY...
tl;dr: Use with_index ActiveRecord's find_each with index If you do not provide a block to find...
To avoid n+1 queries, you want to eager-load associated records if you know you need to access them...
Use reorder to replace an existing order clause with a new expression.
require 'net/http' module Cheat extend self # the magic ingredient def host @host ||= 'http://cheat.errtheblog.com/' end def http @http ||= Net...
edge_rider is Power tools for ActiveRecord relations (scopes). Please note that some of the functions edge_rider provides have...
As you know, assignable_values does not invalidate a record even when an attribute value becomes unassignable. See this example...
EdgeRider 0.3.0 adds support for Rails 4.1 and Ruby 2.1. It forward-ports ActiveRecord::Base.scoped to Rails 4.1.
jQuery plugin that makes it easy to dynamically add and remove records when using ActiveRecord's nested attributes.
ActiveRecord caches results of SQL queries. If you want to discard the cached results for one model, you can call...