When working with ActiveType you will often find it useful to cast an ActiveRecord instance to its extended ActiveType::Record...
Note: ActiveRecord::Base#becomes has a lot of quirks and inconsistent behavior. You probably want to use ActiveType.cast instead.
Capistrano 3 is a major rework of the framework and requires several adjustments to your deploy configuration files. The biggest...
Re-creating a complex ActiveRecord scenario quickly without setting up a full-blown Rails app can come in handy e.g...
Several Rails migration methods accept index: true as an option to create an index. In some cases (like #add_column...
You can freeze any Ruby object to prevent further modification. If you freeze an ActiveRecord and try to set an...
# 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.