Our preferred way of testing ActiveRecord is to simply create/update/destroy the record and then check if the expected behavior has...

Validations should be covered by a model's spec. This card shows how to test an individual validation. This is...

makandra dev

Geordi is now (partially) tested with Cucumber. Yay! geordi cucumber supports a new @solo tag. Scenarios tagged with @solo...

github.com

Using Scenic, you can bring the power of SQL views to your Rails application without having to switch your schema...

When dealing with AR scopes, you can remove conditions, order, etc by using the unscope method. It is available on...

Rails 4 introduced raising an error on pending migrations. This is most annoying when you are crafting a migration but...

github.com

Rails 5 will introduce ActiveRecord::Relation#or. On Rails 4 and 3.2 you can use the activerecord_any_of gem...

It smells. Rethink your code design. Code example with makandra/has_defaults: class Post < ActiveRecord::Base has_defaults tags: [] # field in db...

As you most likely know validates_uniqness_of :foreign_id does not allow nil values by default.

Use form models to handle this problem Or soften the validation to validates_presence_of :parent

Use base_class. This traverses up the hierarchy until it encounters either a class inheriting from ActiveRecord::Base or

In tests, it is sometimes useful to create records with specific ids. On PostgreSQL this can cause problems: Usually, PostgreSQL...

The migration DSL now supports adding and removing foreign keys. They are dumped to schema.rb as well. At this time...

github.com

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.

makandra dev
semaphoreci.com

Capistrano 3 is a major rework of the framework and requires several adjustments to your deploy configuration files. The biggest...

gist.github.com

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...

makandra dev
relishapp.com

RSpec 3 has verifying doubles. This breed of mock objects check that any methods being stubbed are present on an...