...a Ruby on Rails application “booksandreviews.com” exists with three models: class Book < ActiveRecord::Base belongs_to :author has_many :reviews end class Author < ActiveRecord::Base has_many :books end
...Review < ActiveRecord::Base belongs_to :book end 1. Nested Queries Active Record’s where method returns an instance of ActiveRecord::Relation. These relations can be passed to other methods to...
...of a function in an ancestor in a way that doesn’t change its behavior and yet still break one of the descendent classes. Composition Let’s take a completely...
...The zone attribute persists for the rest of the Ruby runtime, potentially causing unexpected behaviour at a later time. Luckily, ActiveSupport solves this problem with the use_zone method. This...
...in a user’s timezone and add 10.5 hours to it: user.time_zone # => Hawaii beginning_of_day = Date.today.in_time_zone(user.time_zone) # => Sun, 03 Apr 2016 00:00:00 HST...
...using exceptions for something like this. Exceptions signal something outside the expected bounds of behavior of the code in question. But if you're running some checks on outside input...
...is because you expect some messages to fail - and if a failure is expected behavior, then you shouldn't be using exceptions. The second problem with code like this is...
Broadly, there are only three approaches to cache expiration. Each has overlapping use cases and varying granularity: Key Based - Everything...
...forward and speaks for itself but why this method is here? Shouldn't be better to create a presenter in UsersController#show action? We could do it and it a...
...time based on the name of the presented instance which in our case @user. Besides that we have method_missing method and it is responsible to handle all helper methods...
...same way is what gives rise to the pattern. Consistency is not the sole benefit of the command pattern. Once you have a command object that can tell a series...
The memento pattern is a software design pattern that provides the ability to restore an object to its previous state...
...associated models: # app/models/user.rb class User < ApplicationRecord has_one :location end # app/models/location.rb class Location < ApplicationRecord belongs_to :user end We want to create one instance of each model using a single...
Rails 5 has added OR method to Active Relation for generating queries with OR clause. >> Post.where(id: 1).or(Post.where...
ActiveRecord’s transaction method takes a block, and will only execute the block and write to your database if no...
...of upvotes and number of comments. One of the possible techniques to support this behavior is defining three types of scopes on your Article model: class Article < ActiveRecord::Base
Have you ever used with_index? Not each_with_index which is similar but slightly different. Did you know that...
...see if a Set includes an object is O(1). Let me clarify: at best and at worst an array is O(n) because it’s always O(n) for...
...search. At best a Set, backed by a Hash, is O(1) which is quite good, O(n) at worst. I think that Ruby developers are lucky but burdened by...
It’s common to use the decorator/presenter pattern to wrap ActiveRecord objects and add view-specific logic. For example:
...his paces. Cletus can speak, and when he does, he moos. Cletus get this behavior from the fact that he is a Cow. When Cletus rests, he chews his cud...
Time doesn't move only forwards Depending on the low level Operating System (OS) settings, Ruby's Time.now uses gettimeofday...
The Policy Objects design pattern is similar to Service Objects, but is responsible for read operations while Service Objects are...
The main purpose of a value object is answer meaningful questions for our application. class Temperature include Comparable def initialize...
} pattern = /(.*)\b(#{expansions.keys.join('|')})\b/i # => st|ct|ln|blvd With block syntax, capture groups become available as block variables $1, $2, etc. Using $2 we can look up the correct...
...Inquirer in Rails code Rails 5 makes use of Array Inquirer and provides a better way of checking for the presence of given variant. Before Rails 5 code looked like...
Consider the following form which has only one input role_id which is accepted through collection_radio_button.
...Array({key: 'value'}) results in [[:key, 'value']] while ActiveSupport’s Array.wrap has less surprising behavior. Array.wrap({key: 'value'})
Some neat tips and tricks for interacting with and parsing JSON responses from an API. Using Net::HTTP require 'net/http...