Loop through multiple arrays in Ruby

Loops are one of the first things you learn in programming. Here is a way to loop through multiple arrays in ruby.

first = Array(1..16)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
second = Array(19..34)
=> [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]

Loop through 2 arrays in Ruby

first.zip(second).each {|b| puts b}
=> [[1, 19],
 [2, 20],
 [3, 21],
 [4, 22],
 [5, 23],
 [6, 24],
 [7, 25],
 [8, 26],
 [9, 27],
 [10, 28],
 [11, 29],
 [12, 30],
 [13, 31],
 [14, 32],
 [15, 33],
 [16, 34]]

Loop through 3 arrays in Ruby

first = Array(1..16)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
second = Array(19..34)
=> [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
third = Array(37..52)
=> [37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
first.zip(second, third).each {|b| puts b}
=> [[1, 19, 37],
 [2, 20, 38],
 [3, 21, 39],
 [4, 22, 40],
 [5, 23, 41],
 [6, 24, 42],
 [7, 25, 43],
 [8, 26, 44],
 [9, 27, 45],
 [10, 28, 46],
 [11, 29, 47],
 [12, 30, 48],
 [13, 31, 49],
 [14, 32, 50],
 [15, 33, 51],
 [16, 34, 52]]

Voila!

 Using Serialized data in Rails ActiveRecord

In the previous article, How to Add a Blob using Rails ActiveRecord, we needed to use a blob to store large data. We added a blob data type via the migration file.

class AddMonthsToNyC240 < ActiveRecord::Migration
  def self.up
    add_column :ny_c240s, :weeks52, :binary, :limit => 10.megabyte
  end
  def self.down
    remove_column :ny_c240s, :weeks52 end
  end
end

We migrated the file:

== 20161029155357 AddMonthsToNyC240: migrating ================================
-- add_column(:ny_c240s, :weeks52, :binary, {:limit=>10485760})
   -> 0.1093s
== 20161029155357 AddMonthsToNyC240: migrated (0.1095s) =======================

Now we need to add some functions to the ActiveRecord Model

class NyC240 < ActiveRecord::Base
  serialize :weeks52
end

Voila! You can now save data here and ActiveRecord will automatically know if it is a Hash or Array.

 How to Add a Blob using Rails ActiveRecord

There are ton of data types you can use on multiple RDBMS. You can use a text, integer, enum, string or blob. In Rails ActiveRecord, it is normal to use string. String is in fact the default data type(a column will be become a string if you don’t explicitly define another data type).

How to use BLOB as the data type in Rails ActiveRecord

class AddMonthsToNyC240 < ActiveRecord::Migration
  def self.up
    add_column :ny_c240s, :weeks52, :binary, :limit => 10.megabyte
  end
  def self.down
    remove_column :ny_c240s, :weeks52
  end
end
== 20161029155357 AddMonthsToNyC240: migrating ================================
-- add_column(:ny_c240s, :weeks52, :binary, {:limit=>10485760})
   -> 0.1093s
== 20161029155357 AddMonthsToNyC240: migrated (0.1095s) =======================

Blob created!

 Manually Confirm User with Devise

There are times when you need to manually confirm users. One reason is that you didn’t realize that you need to confirm the user but you don’t have access to the account. Another reason is that you just need users to be confirmed on your local machine.

Created User

[5] pry(main)> u = User.create(email: 'mememe@yahoo.com', password: 'p@$$w0rd', company_id: 2, first_name: 'Me', last_name: 'Me', role: 'admin', facility_id: 1000)
   (0.4ms)  BEGIN
  User Exists (0.6ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'mememe@yahoo.com' LIMIT 1
  User Load (6.6ms)  SELECT  "users".* FROM "users" WHERE "users"."confirmation_token" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["confirmation_token", "452c89a4307e079caf88ff9b9ac83b6486871e70eebfdad20d7b910718c17bab"]]
  SQL (24.9ms)  INSERT INTO "users" ("email", "encrypted_password", "company_id", "first_name", "last_name", "role", "facility_id", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"  [["email", "mememe@yahoo.com"], ["encrypted_password", "$2a$10$Ar6Un0ippVPKNPBc8enPReo9ZUg9VhhKlvRGVQBcshR.kFlj5iMxW"], ["company_id", 2], ["first_name", "Me"], ["last_name", "Me"], ["role", "admin"], ["facility_id", 1000], ["created_at", "2016-10-05 06:46:20.077982"], ["updated_at", "2016-10-05 06:46:20.077982"], ["confirmation_token", "yq4kessd4D71VEZrzXSV"], ["confirmation_sent_at", "2016-10-05 06:46:20.310942"]]

Manually Confirming a User

User.last.confirm!
DEPRECATION WARNING: confirm! is deprecated in favor of confirm. (called from __pry__ at (pry):8)
   (0.2ms)  BEGIN
  SQL (6.9ms)  UPDATE "users" SET "confirmed_at" = $1, "updated_at" = $2 WHERE "users"."id" = $3  [["confirmed_at", "2016-10-05 07:10:09.147281"], ["updated_at", "2016-10-05 07:10:09.152368"], ["id", 29]]
   (6.2ms)  COMMIT
=> true

User Confirmed!

confirm! is deprecated and you must use confirm