Shortcut Blocks with Symbol to_proc in Rails 5

Objective

To learn how to use Symbol#to_proc shortcut with ActiveRecord in Rails 5.

Steps

Step 1

Let's load all the projects.

 projects = Project.all
  Project Load (1.3ms)  SELECT "projects".* FROM "projects"
 => #<ActiveRecord::Relation [#<Project id: 1, name: "Wealth Building", created_at: "2016-03-10 01:26:12", updated_at: "2016-03-10 01:26:12">, #<Project id: 2, name: "Wealth Building", created_at: "2016-03-10 01:26:36", updated_at: "2016-03-10 01:26:36">, #<Project id: 3, name: "Wealth Building", created_at: "2016-03-10 01:28:27", updated_at: "2016-03-10 01:28:27">, #<Project id: 4, name: "Be Happy", created_at: "2016-03-10 01:28:27", updated_at: "2016-03-10 01:28:27">]> 
 > names = projects.collect {|p| p.name}
 => ["Wealth Building", "Wealth Building", "Wealth Building", "Be Happy"] 

Step 2

We have 4 projects and 8 tasks.

 Project.count
   (0.1ms)  SELECT COUNT(*) FROM "projects"
 => 4 
 Task.count
   (0.1ms)  SELECT COUNT(*) FROM "tasks"
 => 8 

Step 3

We can use Symbol#to_proc method to get the names of all projects.

 projects.collect(&:name)
 => ["Wealth Building", "Wealth Building", "Wealth Building", "Be Happy"] 

Step 4

We can check if all projects are valid.

 projects.all?(&:valid?)
 => true 

Step 5

We can check if any of the project is valid.

 projects.any?(&:valid?)
 => true 

Step 6

We can save all the projects using the Symbol#to_proc shortcut.

 projects.each(&:save!)
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
 => [#<Project id: 1, name: "Wealth Building", created_at: "2016-03-10 01:26:12", updated_at: "2016-03-10 01:26:12">, #<Project id: 2, name: "Wealth Building", created_at: "2016-03-10 01:26:36", updated_at: "2016-03-10 01:26:36">, #<Project id: 3, name: "Wealth Building", created_at: "2016-03-10 01:28:27", updated_at: "2016-03-10 01:28:27">, #<Project id: 4, name: "Be Happy", created_at: "2016-03-10 01:28:27", updated_at: "2016-03-10 01:28:27">] 

Step 7

We can list all the names of the projects using the ampersand shortcut:

all_names = Project.all.collect(&:name)
  Project Load (0.2ms)  SELECT "projects".* FROM "projects"
 => ["Wealth Building", "Wealth Building", "Wealth Building", "Be Happy"] 

Step 8

We can use the ampersand shortcut in the project model.

class Project < ApplicationRecord
  has_many :tasks

  def self.all_names
    all.collect(&:name)
  end
end

Summary

In this article, you learned how to use the Symbol#to_proc shortcut operator ampersand.


Related Articles


Create your own user feedback survey