Today I Learned

A Hashrocket project

172 posts about #rails

HTTP Request Methods Limiting Arguments

Check out this RSpec test from a Rails application:

# spec/controllers/chapters_controller_spec.rb
describe ChaptersController do
  describe '#create' do
    it 'creates a chapter' do
      expect do
        post :create, chapter: FactoryGirl.attributes_for(:chapter)
      end.to change(Chapter, :count).by(1)
    end
  end
end

The code after :create will soon be deprecated.

5.0+ versions of Rails will only accept keyword arguments for ActionController::TestCase HTTP request methods.

Here’s that same test, minus the deprecation warnings I encountered upgrading to the first Rails 5 release candidate:

# spec/controllers/chapters_controller_spec.rb
describe ChaptersController do
  describe '#create' do
    it 'creates a chapter' do
      expect do
        post :create, params: { chapter: { title: 'Change', body: 'Change is coming!' } }
      end.to change(Chapter, :count).by(1)
    end
  end
end

Rails 5 - Use limit on ActiveRecords with OR

Rails 5 comes released a new ActiveRecord or but it still have some constraints you should know.

limit, offset, distinct should be on both queries, otherwise:

irb(main):001:0> Session.where(user_id: 1).or(
                   Session.where(provider: 'github').limit(10)
                 ).to_sql
=> ArgumentError: Relation passed to #or must be structurally compatible.
   Incompatible values: [:limit]

You need to add limit to both sides of the query, like:

irb(main):002:0> puts Session.where(user_id: 1).limit(10).or(
                   Session.where(provider: 'github').limit(10)
                 ).to_sql
=> "SELECT  "sessions".*
    FROM "sessions"
    WHERE ("sessions"."user_id" = 1 OR "sessions"."provider" = 'github')
    LIMIT 10"

Rails 5 API and CORS

So if you have your frontend and backend on different domains you’ll need to allow CORS (cross-origin HTTP request).

Rails 5 with --api mode will prepare the setup for you. You just need to uncomment the following:

# Gemfile
gem 'rack-cors'
# config/initializers/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'localhost:4200'
    resource '*',
      headers: :any,
      methods: %i(get post put patch delete options head)
  end
end

Time Travel by Rails

Rails have travel, travel_to, travel_backTesting Helper Methods to travel in time and then test your app with exact dates and times.

include ActiveSupport::Testing::TimeHelpers

Time.current
=> Tue, 17 May 2016 12:31:52 UTC +00:00

travel_to Time.new(2004, 11, 24, 01, 04, 44)
Time.current
=> Wed, 24 Nov 2004 06:04:44 UTC +00:00

travel_back
Time.current
=> Tue, 17 May 2016 12:32:17 UTC +00:00

Another nice alternative is to use gems like:

Rails 5 API mode

You can build a rails API with the --api mode on Rails 5.

rails _5.0.0.rc1_ new rails5-api --api

Gems removed with api mode:

  • coffee-rails
  • jquery-rails
  • sass-rails
  • uglifier
  • turbolinks
  • jbuilder

Files removed with api mode:

  • app/assets
  • lib/assets
  • vendor/assets
  • app/helpers
  • app/views/layouts/application.hmtl.erb

ApplicationController

app/controller/application_controller.rb has changed to inherit from ActionController::API

Rack middleware list changed with api mode:

5 middlewares were removed, including Cookies and Flash.

use Rack::MethodOverride
use WebConsole::Middleware
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash

Check their changelog

Rails 5 changed belongs_to default validation

New Rails 5 applications comes with this initializer:

# config/initializers/active_record_belongs_to_required_by_default.rb
Rails.application.config.active_record.belongs_to_required_by_default = true

So now, belongs_to associations require presence by default.

In order to make this relation optional you can change the initializer value for the whole app, or add optional: true to your relation:

belongs_to :user, optional: true

That’s the code change.

Rails 5 deprecation warning for Controller tests

Rails 5 has changed its ActionController::TestCase::Behavior to methods we use in controller tests and now you need to specify params key to the requests.

So if you had this in you tests:

get :show, id: 1

You’ll need to change to:

get :show, params: { id: 1 }

There is a warning message like this:

ActionController::TestCase HTTP request methods will accept only
keyword arguments in future Rails versions.
Examples:
  get :show, params: { id: 1 }, session: { user_id: 1 }
  process :update, method: :post, params: { id: 1 }

Even if you use Rpsec you will need to change because rspec-railsuses this module to create the requests to the controllers.

Perform SQL Explain With ActiveRecord

Want to check out the performance characteristics of some SQL query from within a Pry session? ActiveRecord allows you to perform a SQL explain on any ActiveRecord::Relation object. After chaining some Arel functions together, add an #explain.

Here is an example:

Recipe.all.joins(:ingredient_amounts).explain
  Recipe Load (0.9ms)  SELECT "recipes".* FROM "recipes" INNER JOIN "ingredient_amounts" ON "ingredient_amounts"."recipe_id" = "recipes"."id"
=> EXPLAIN for: SELECT "recipes".* FROM "recipes" INNER JOIN "ingredient_amounts" ON "ingredient_amounts"."recipe_id" = "recipes"."id"
                                 QUERY PLAN
----------------------------------------------------------------------------
 Hash Join  (cost=1.09..26.43 rows=22 width=148)
   Hash Cond: (ingredient_amounts.recipe_id = recipes.id)
   ->  Seq Scan on ingredient_amounts  (cost=0.00..21.00 rows=1100 width=4)
   ->  Hash  (cost=1.04..1.04 rows=4 width=148)
         ->  Seq Scan on recipes  (cost=0.00..1.04 rows=4 width=148)
(5 rows)

source

Rails: Start Resque Worker for Async Mail Delivery

QUEUE=mailers rake resque:work

Assuming you have properly configured Resque to work with ActiveJob, you will now have a worker waiting to execute mail delivery requests. Read more about how to execute these deliveries to take full advantage of the feature.

If you want to run the worker as a background process simply set the BACKGROUND environment variable before running the rake task:

BACKGROUND=1 QUEUE=mailers rake resque:work

Create Migrations Faster with Vim-Rails

Not only can we create database migrations with vim-rails, it’s faster than using a generator. I counted the steps today and realized I was wasting precious keystrokes by underutilizing this great plugin.

Here’s how I was setting up a migration:

  • Generate the migration:
$ rails g migration add_diameter_to_wheels
  • Load the migration in Vim with :Emigration (vim-rails)
  • Replace change method with up and down methods

Here’s my improved method:

  • Generate a migration (in Vim) with :Emigration AddDiameterToWheels!
  • Create up and down methods

Less steps and characters, and best of all, we never have to leave Vim.

h/t Chris Erin

Demodulize A Class Name

If you call .class.name on an instance of some class, the fully qualified name will be returned, module names and all. Consider the following example class:

module One
  module Two
    class Three
      ...
    end
  end
end
> One::Two::Three.new.class.name
#=> "One::Two::Three"

If you just want the unqualified class name; modules not included, you can use the #demodulize method provided by ActiveSupport.

> One::Two::Three.new.class.name.demodulize
#=> "Three"

Rails nested forms

You can use rails Nested Forms even with plain form objects.

You just need to use form.fields_for and then declare the params name and the object for validation errors.

view

= form_for @foo_form, as: :foo, url: foo_path do |form|
  = form.text_field :name
  = form.fields_for :bar_params, @foo.bar do |nested_form|
    = nested_form.text_field :description

form

class FooForm
  include ActiveModel::Model

  attr_accessor :name, :bar_params, :bar
  validates :name, presence: true

  def initialize(attributes = {})
    super attributes

    self.bar  = BarForm.new(bar_params)
  end

  def save
    valid? && Foo.create(name: name)&& bar.save
  end
end
class BarForm
  include ActiveModel::Model

  attr_accessor :description
  validates :description, presence: true

  def save
    valid? && Bar.create(description: description)
  end
end

controller

class FooController < ApplicationController
  def edit
    @foo_form = FooForm.new
  end

  def create
    @foo_form = FooForm.new(params[:foo])
    if @foo_form.save
      redirect_to root_path
    else
      render :edit
    end
end

So the bar params will be scoped into bar_params node, and the validations will work.

Write and Read Rails Attributes with Aliases

Lately I’ve been using a nice alias for writing record attributes in Rails, []=. Behold:

[1] pry(main)> d = Developer.new
=> #<Developer:0x007ff7130f8ef0
[2] pry(main)> d[:username] = 'isaacemard'
=> "isaacemard"
[3] pry(main)> d.username
=> "isaacemard"

That’s all it takes. There’s a getter variant, too:

[4] pry(main)> d[:username]
=> "isaacemard"

Here’s how I used it today:

# app/models/developer.rb
before_create :generate_slug

def generate_slug
   self[:slug] ||= SecureRandom.hex(5)
end

To me this code reads a lot more nicely than write_attribute(attr_name, value). These aliases have been a part of Rails since at least 2012.

Source

h/t Mike Chau and Chris Erin

Attach A File With Capybara

There are two ways to attach a file with Capybara. The more conventional way is with the attach_file method.

Assuming there is a form with a file input similar to the following:

<label for='data-file'>Data File</label>
<input type='file' name='data-file' />
attach_file('data-file', 'path/to/file.csv')

The first argument to attach_file is a locator which refers to the name attribute on the input tag.

If for some reason there is no name attribute on the input tag, the file can be attached with a standard find and set.

find('form input[type="file"]').set('path/to/file.csv')

Custom Validation Message

When using Rails validations, a standard error message will be provided whenever there is a violation. Consider the scenario when there is a uniqueness validation on the email attribute and it is violated:

# User model
validates_uniqueness_of :email

# Users controller
new_user.errors.full_messages
#=> ["Email has already been taken"]

Sometimes you don’t want the default validation message. The validation declaration can be given a message option to specify an alternate validation message.

# User model
validates_uniqueness_of :email, message: 'is not available'

# Users controller
new_user.errors.full_messages
#=> ["Email is not available"]

Keep in mind that full_messages will prepend the model name to the front of the message. You’ll want to ensure that the resulting message is coherent.

Advanced Search with Textacular

Textacular exposes full text search capabilities from PostgreSQL, extending ActiveRecord with scopes making search easy and fun!

We wanted an advanced search, but that should also work as autocomplete.

So we end up using the gem textacular and the PostgreSql tsquery with the :* for prefixed searches

Game.advanced_search(title: 'street fi:*')

Thanks @mrmicahcooper!

references

Truncate Almost All Tables

The database_cleaner gem is a handy way to make sure you have a consistent database context for each test example or suite. One database_cleaner strategy that can be used is the truncation strategy. This truncates the data from all the tables by default. This is not ideal for fixed tables that contain domain-specific data because you end up having to do way more test setup than should be necessary. Fortunately, specific tables can be excepted by the truncation strategy using the except option.

For instance, if we have a standard set of roles for users of our application, we can except that table from truncation with a line like the following in our rails_helper.rb file:

DatabaseCleaner.strategy = :truncation, {:except => %w[roles]}

Interact with Rails via Runner

The rails runner feature of the Ruby on Rails command line interface is pretty awesome.

The documentation states:

runner runs Ruby code in the context of Rails non-interactively.

Use it like the ruby command to execute Ruby code in the context of your Rails environment. Take this file:

# rails_runner_in_action.rb
puts Developer.count # 5
puts Post.count # 40
puts Channel.count # 17

And run it with:

$ rails runner rails_runner_in_action.rb
5
40
17

It also runs Ruby code right in the terminal, so this works (rails r is an alias):

$ rails r "puts Developer.count"
5

http://guides.rubyonrails.org/command_line.html#rails-runner

after_commit callback in active_record

We use ActiveRecord callbacks at times to sync data between systems but if you’re using transactions you don’t want to have called a callback like after_create and then have the whole transaction roll back, then you’d have out of sync systems! Rails conveniently includes an after_commit hook for such a case.

class Fruit < ActiveRecord::Base
  after_commit :sync_to_fruitstand
end

ActiveRecord::Base.transaction do
  Fruit.create(name: 'Banana')
  Fruit.create(name: 'Kiwi')
  Fruit.create(name: 'Rasberry')
end

Now, the callback is called (for all 3 times) only when the final Fruit creation for a Rasberry succeeds.

Set Schema Search Path

By default the schema search path for a PostgreSQL database is going to be "$user", public. Tables created by a Rails migration are going to end up on the public schema by default. If your application has other schemas in play, then you may want to ensure that those schemas are also on the schema search path. This can be accomplished by adding the schema_search_path setting to your database.yml file. For instance, to include both the legacy and public schema in the Postgres search path, add the following line:

schema_search_path: "legacy,public"

h/t Jack Christensen

Change Rails validation message completely

Rails always appends field name to validation messages, and when setting message: on a validation statement it gets appended to the humanized name of the field.

If you want to change the validation message completely use the locales file:

# config/locales/en.yml
en:
  activerecord:
    attributes:
      user:
        email: "E-mail address"
    errors:
      models:
        user:
          attributes:
            email:
              blank: "is required"

This was particularly useful with the ValidatesTimeliness gem which does not support a lambda.


🎉Happy 500 post to TIL 🎉

Rails Sandbox 🏖

When you are working with a complicated database structure, and find yourself needing to debug a complex or dangerous (delete) action, you might be hesitant to experiment. Keep experimenting! Don’t want to setup all that data again? No worries. You can use a sandbox $ rails c -s

Usage: rails console [environment] [options]
  -s, --sandbox      Rollback database modifications on exit.

The sandbox flag will keep all database changes in a database transaction when you start the rails console and automatically issue a rollback when you quit the console.

Generate a Rails Secret Key

Have you ever wondered about those secret keys found in config/secrets.yml of your Rails app? The comments generated in that file describe the keys as such:

‘Your secret key is used for verifying the integrity of signed cookies.’

Great… but what if they become compromised? Or we need to change them? We can generate new ones.

Rails provides rake secret for just this purpose.

The source code is here. The code simply requires SecureRandom and spits out a string. If you want to be really clever, you can pipe the string directly into your Vim buffer for the config file, with :.! rake secret.

Check out rake -T secret inside a Rails root directory for more information.

Show Rails Routes With Pry

In Show Rails Models With Pry, I showed that pry-rails comes with some handy console commands. In addition to being able to list all your Rails models, you can list all the routes for your application using show-routes.

I get the following output by using that command in a small blog project:

> show-routes
              Prefix Verb   URI Pattern                     Controller#Action
                root GET    /                               application#index
markdownify_articles POST   /articles/markdownify(.:format) articles#markdownify
            articles POST   /articles(.:format)             articles#create
         new_article GET    /articles/new(.:format)         articles#new
        edit_article GET    /articles/:id/edit(.:format)    articles#edit
             article GET    /articles/:id(.:format)         articles#show
                     PATCH  /articles/:id(.:format)         articles#update
                     PUT    /articles/:id(.:format)         articles#update
               users POST   /users(.:format)                users#create
            new_user GET    /users/new(.:format)            users#new
                user GET    /users/:id(.:format)            users#show
            sessions POST   /sessions(.:format)             sessions#create
         new_session GET    /sessions/new(.:format)         sessions#new
             session DELETE /sessions/:id(.:format)         sessions#destroy
              signin GET    /signin(.:format)               sessions#new
                     POST   /signin(.:format)               sessions#create
              signup GET    /signup(.:format)               users#new

Set a Default Scope

ActiveRecord provides default_scope, which you can use to control how records are returned. An example:

# app/model/chapter.rb
class Chapter < ActiveRecord::Base
  default_scope { order(:title) }
end

Now, instead of this (ordered by ID):

Chapter.all.pluck(:title).take(5)
=> ["BALINESE CAUSE",
"BUILDING RAT",
"TAIL",
"LILAC MERCURY",
"PUMP SQUARE"]

We get this:

Chapter.all.pluck(:title).take(5)
=> ["AFGHANISTAN COAT",
"ALCOHOL GONG",
"ANTARCTICA BULL",
"ASPARAGUS",
"BALINESE CAUSE"]

Beware, this decision can lead to inexplicable behavior down the road. Use with caution.

One use case I’ve heard is soft deletes. We might not want soft-deleted records appearing in .all:

# app/model/chapter.rb
class Chapter < ActiveRecord::Base
  default_scope { where('soft_deleted is false') }
end

h/t Brian Dunn

Strong Parameters with arrays

Need to permit a strong parameter that can either be an array or a string? Just permit it twice!

# This is just for shortened syntax for this TIL.
class Param < ActionController::Paramaeters
end 

Param.new({foo: "bar"}).permit(:foo) 
#=> { "foo" => "bar" }
Param.new({foo: "bar"}).permit(foo: []) 
#=> { }

Param.new({foo: ["bar"]}).permit(foo: []) 
#=> { "foo" => ["bar"] }
Param.new({foo: ["bar"]}).permit(:foo)
 #=> { }

Param.new({foo: ["bar"]}).permit(:foo, foo: [])
 #=> { "foo" => ["bar"] }
Param.new({foo: "bar"}).permit(:foo, foo: [])
 #=> { "foo" => "bar"}

Show Rails Models With Pry

With the pry-rails gem, you get some extra goodies in the Rails console for your project. One of those goodies is show-models, a command for printing out a list of all models in the rails project. Add and bundle the pry-rails gem, run rails c, and then run show-models to give it a go.

> show-models
Pokemon
  id: integer
  name: string
  level: integer
  pokemon_type: varchar
  belongs_to Trainer
  created_at: datetime
  updated_at: datetime
Trainer
  id: integer
  name: string
  has_many Pokemons

Grep Rails Routes

This week I found a treasure inside the Hashrocket Dotmatrix; behold:

# .zshrc
alias groutes='rake routes | grep $@'

This aliases rake routes | grep [foo], which I frequently use to search through big Rails routes files.

It looks like the Rails core team is responding to this pattern in Rails 5:

https://github.com/rails/rails/issues/18902

In the meantime, patch it yourself and reap the benefits.

h/t Dorian Karter & Jon Allured

Params Includes Submission Button Info

When a form is submitted for a Rails app, the respective controller action will have access to a variety of information in the params hash. Included is an entry with the name and value of the button that submitted the form. By default, Rails will give the name commit to a submission button.

<%= f.submit %>
# results in:
<input type="submit" name="commit" value="Submit">Submit</input>

The corresponding create action will have parameters that include that submission button’s info:

# in create action
> params['commit']
=> 'Submit'

This is useful when you have multiple buttons that submit the same form, but should have slightly different results in the corresponding action. Differentiating becomes easy when you can easily check which was used to submit the form. No javascript required.

Inspect Your Previous Changes

Today I found a nice ActiveModel method for inspecting previous changes, previous_changes (SQL has been removed):

[1] pry(main)> k = Kit.first
=> #<Kit:0x007fb487cec988 id: 1, name: "Foo", ... >
[2] pry(main)> k.name = 'Bar'
=> "Bar"
[3] pry(main)> k.save
=> true
[4] pry(main)> k.previous_changes
=> {"name"=>["Foo", "Bar"], "updated_at"=>[Fri, 20 Nov 2015 17:57:50 UTC +00:00, Fri, 20 Nov 2015 18:00:51 UTC +00:00]}

If nothing was updated, the method returns an empty hash.

[1] pry(main)> k = Kit.first
=> #<Kit:0x007fb487dc47e8 id: 1, name: "Foo", ... >
[2] pry(main)> k.name = 'Foo'
=> "Foo"
[3] pry(main)> k.save
=> true
[4] pry(main)> k.previous_changes
=> {}

I can imagine many practical and potentially foolish applications for this information… planning to work it into a test at some point. ActiveModel::Dirty is full of interesting methods like this.

http://apidock.com/rails/ActiveModel/Dirty/previous_changes

Test Your Migrations

Today I discovered rake db:migrate:redo. This command rolls back a migration, and then runs the migration again. It’s a convenience method for checking your work and iterating through complex migrations.

The Hashrocket Dotmatrix leverages this via the twiki method:

twiki () {
  rake db:migrate && rake db:migrate:redo && rake db:test:prepare
}

Migrate the database, then roll it back, then migrate it again, then rebuild the test database. If anything is wrong going up or coming down, this will catch it.

h/t Josh Branchaud

Rails restore_attributes

Today I found a useful method in ActiveRecord, restore_attributes.

This method restores attributes on a dirty model. Use it when you are hacking in the Rails console and want to quickly return to a clean slate.

Dirty the record:

pry> p = Post.first
=> #<Post:0x007f8462b09eb8
 id: 106,
 created_at: Sun, 09 Feb 2014 17:15:01 CST -06:00,
 url_slug: "hello-world">

pry> p.url_slug = 'foobar'
=> "foobar"

pry> p.created_at = Time.now
=> 2015-10-13 11:41:37 -0500

pry> p
=> #<Post:0x007f8462b09eb8
 id: 106,
 created_at: Tue, 13 Oct 2015 11:41:37 CDT -05:00,
 url_slug: "foobar">

And restore:

pry> p.restore_attributes
=> ["url_slug", "created_at"]

pry> p
=> #<Post:0x007f8462b09eb8
 id: 106,
 created_at: Sun, 09 Feb 2014 17:15:01 CST -06:00,
 url_slug: "hello-world">

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/dirty.rb#L191

ActiveRecord Dirty module

Rails provides a way to check if your model’s attributes have changed since initially loading it from database. It also provides convenient methods for checking what the values were before the change using #{attribute_name}_was as in the example below.

# A newly instantiated object is unchanged:
person = Person.find_by_name('Uncle Bob')
person.changed?       # => false
  
# Change the name:
person.name = 'Bob'
person.changed?       # => true
person.name_changed?  # => true
person.name_was       # => 'Uncle Bob'
person.name_change    # => ['Uncle Bob', 'Bob']
person.name = 'Bill'
person.name_change    # => ['Uncle Bob', 'Bill']
  
# Save the changes:
person.save
person.changed?       # => false
person.name_changed?  # => false

# Assigning the same value leaves the attribute unchanged:
person.name = 'Bill'
person.name_changed?  # => false
person.name_change    # => nil
  
# Which attributes have changed?
person.name = 'Bob'
person.changed        # => ['name']
person.changes        # => { 'name' => ['Bill', 'Bob'] }

Rails destroy

The rails generate (g) command is useful for quickly creating the building blocks of a Rails app. Until you misspell or otherwise mess up a command:

% rails g model blurg_post
      invoke  active_record
      create    db/migrate/20151006162244_create_blurg_posts.rb
      create    app/models/blurg_post.rb
      invoke    rspec
      create      spec/models/blurg_post_spec.rb

‘Blurg post’? I meant ‘blog post’. Luckily, we can remove those auto-generated files as fast as we created them, with destroy (d).

% rails d model blurg_post
      invoke  active_record
      remove    db/migrate/20151006162244_create_blurg_posts.rb
      remove    app/models/blurg_post.rb
      invoke    rspec
      remove      spec/models/blurg_post_spec.rb

http://edgeguides.rubyonrails.org/command_line.html#rails-destroy

deep_munge, I hardly knew ye.

Rails 4.1.8 has a method called deep_munge which is intended to massage params to convert empty arrays into nils in response to security issues like http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-2660.

This can be tricky when sending in a json string as post data that may have some values that should be empty arrays. Those empty arrays will get converted to nil.

To get around that rails 4.1.8 added a configuration option

config.action_dispatch.perform_deep_munge = false

Rails 5.0 intends to not have the same sql injection vulnerabilities and so have removed the deep_munge method that would change an empty array value to nil but have left in the configuration option which produces behavior best described by looking at the tests.

Rails source

All Rails http verbs are UPPERCASE

Rails supports many http verbs, and in the rails source code they’re handily organized by ietf rfc.

    RFC2616 = %w(OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT)
    RFC2518 = %w(PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK)
    RFC3253 = %w(VERSION-CONTROL REPORT CHECKOUT CHECKIN UNCHECKOUT MKWORKSPACE UPDATE LABEL MERGE BASELINE-CONTROL MKACTIVITY)
    RFC3648 = %w(ORDERPATCH)
    RFC3744 = %w(ACL)
    RFC5323 = %w(SEARCH)
    RFC4791 = %w(MKCALENDAR)
    RFC5789 = %w(PATCH)

source

Notice though that they’re all upper case, so if you try to make an http request with lowercase patch, then you’ll get a crazy UnknownHttpMethod error.

Yo dawg, I heard you like Hash#merge

Yo dawg…I heard you like Hash#merge…

With #reverse_merge, you can have a new hash in your old hash, and keep the keys from your old hash in your new hash.

hash_one = { a: 1, b:2 }
hash_one.merge({ a:2, b:3 }) # => { a:2, b:3 } 

So the hash supplied as a parameter will override hash_one’s keys and values.

If you’re in Rails and you don’t want your existing hash keys to get overwritten, use your new friend #reverse_merge

Now, you can merge the other way around!

hash_one = { a: 1, b:2 }
hash_one.reverse_merge({ a:2, b:3, c:3 }) # => { a:1, b:2, c:3 } 

Boom, keep your keys.

Autosave False On ActiveRecord Associations

A relationship between two ActiveRecord models can be established with a has_one or has_many association. This relationship has some implications. By default, saving a record will also save the associated records that have since been built. Consider this example of users that have many posts.

> u = User.first
#=> #<User ...>
> u.posts
#=> []
> u.posts.build(title: "Some Title", content: "This is a post")
#=> #<Post ...>
> u.save
#=> true
> u.posts(reload: true)
#=> [#<Post ...>]

When the user is saved, the associated post that was built for that user also gets saved to the database.

If the association is instead defined with the autosave option set to false, then saving a record will not cause associated records to also be saved. The associated records will need to be saved explicitly. Consider the same example from above, but with has_many posts, autosave: false.

> u = User.first
#=> #<User ...>
> u.posts
#=> []
> u.posts.build(title: "Some Title", content: "This is a post")
#=> #<Post ...>
> u.save
#=> true
> u.posts(reload: true)
#=> []

The post wasn’t saved with the user and it wasn’t saved explicitly, so it isn’t persisted to the database.

Transaction within a transaction

When using transactional fixtures in rspec, you’re likely to always be in a transaction. When that’s the case, transactions in you’re code don’t necessarily behave as you’d expect.

def update
  ActiveRecord::Base.transaction do
    Thing.find(params[:id]).update_attributes(color: 'red')
    raise ActiveRecord::Rollback
  end
end

In this case the transaction won’t rollback the changes to Thing, and if you’re testing that rollback in rspec, you’ll be very confused.

While not the perfect solution, it’s possible to force this transaction to behave how you’d like it to with the requires_new: true named argument which makes the transaction a ‘sub-transaction’.

  ActiveRecord::Base.transaction(requires_new: true) do
  end

documentation

ActiveRecord::Relation size vs count

An array in ruby has 3 methods that do the same thing. size, count, and length all return the number of items in an array.

An ActiveRecord::Relation however uses them a bit differently. count is always going to run a query in the database while size will return the number of items in the collection based on the objects currently in the object graph.

> songs  = Songs.all
> songs.size
10
> songs.count
SELECT count(*) FROM songs;
10

Creating an object from an ActiveRecord::Relation

Rails allows us devs to create a record from an ActiveRecord::Association like so:

> wheel = Car.find_by(type: 'Mazda').wheels.build
> wheel.car_id
1

And in this way we can create a wheel associated with a specific car.

But we can also create an object from an ActiveRecord::Relation that allows us to predefine attributes.

> spare_wheel = car.wheels.where(spare: true).build
> spare_wheel.spare
true

Hash Slicing

Rails’ ActiveSupport adds #slice and #slice! to the Hash class. The interface of these two methods seems a little inconsistent though.

> {a: 1, b: 2, c: 3}.slice(:a)
=> {:a=>1}

The #slice method returns what is being sliced.

> {a: 1, b: 2, c: 3}.slice!(:a)
=> {:b=>2, :c=>3}

The #slice! method, on the other hand, returns what is being excluded.

Explicitly Set Table Name

ActiveRecord includes a table_name method that infers your database table name based on a class name. For an empty model called AlternativePost, here’s what it comes up with:

[0] > AlternativePost.table_name
=> "alternative_posts"

If this isn’t right, you’re in trouble (Postgres example):

[1] > AlternativePost.new
PG::UndefinedTable: ERROR:  relation "alternative_posts" does not exist

Luckily, you can set the table name explicitly with the table_name= method.

class AlternativePost < ActiveRecord::Base
  self.table_name = 'posts'
end

Now Rails knows which database table to refer to:

[2] > AlternativePost.table_name
=> "posts"
[3] > AlternativePost.new
=> #<AlternativePost:0x007f8554dcbd98 id: nil, title: nil...

A practical application would be a model Post that corresponds to a table organized inside a Postgres schema admin.posts.

Return an Empty Active Record Collection

You can use .none in a scope to short circuit the query in the event you don’t have all the data.

Imagine this query but the project_type on a Project is nil


class User

  scope :active -> { where(archived: nil }

  scope :by_project, -> (project) do
    return none unless project.type.present?
    where(project_guid: project.guid, role: project.type)
  end

end

Just return none.

The cool thing about this is it’s chainable. So you can still do something like:


project = Project.new(project_type: nil)

User.by_project(project).active

Ignore Poltergeist JavaScript Errors

Poltergeist with PhantomJS (<2.0) does not support JavaScript’s bind() method. This means that when executing an integration test that exercises JavaScript with the bind() method, an error will occur. If you cannot simply upgrade to a version of PhantomJS that supports bind(), then what can you do?

Ignore the error!

This can be achieved by placing the following rescue block in the appropriate place.

rescue Capybara::Poltergeist::JavascriptError

Use this in moderation. You want to make sure you don’t ignore actual JavaScript errors.

source

Select Value For SQL Counts

If you are like me and prefer writing raw SQL over the Arel DSL for counting stuff in your database, then the select_value method will come in handy. Write a command similar to the following with a type cast to get the count of whatever.

> sql = 'select count(*) from posts where published_at is not null'
=> "select count(*) from posts where published_at is not null"
> ActiveRecord::Base.connection.select_value(sql).to_i
   (0.6ms)  select count(*) from posts where published_at is not null
=> 42

Writing raw SQL for a simple query like this hardly seems like a win. However when a count query starts to involve joins or other fanciness, I find it much clearer to reason about the raw SQL.

Make fonts work with asset pipeline on production

If you encounter a problem where fonts don’t work on production when using Bourbon CSS toolset, you need to replace the font declaration from:

font-face("great-font","/assets/greatfont")

to

@include font-face("greatfont", "greatfont", $asset-pipeline: true)

This will use asset pipeline fingerprinted URL in the font declaration and should work on all environments.

P.S. With rails 4.0+ you only need to put font files in app/assets/fonts and they will be fingerprinted and zipped.