• Home
  • Akiko Yokoyama
  • Contact
  • Feed
ja | en |

Rails console command tips

I’m going to write about the things the Rails console can do that I regularly google.

Ref. Introduction ActiveRecord - Qiita

A Rails console thing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ rails c

pp Category.select("id, name").first.to_json
D, [2018-02-02T17:24:24.080967 #78025] DEBUG -- : Category Load (0.3ms) SELECT id, name FROM `categories` WHERE `categories`. `deleted_at` IS NULL ORDER BY `categories`. `id` ASC LIMIT 1
{"id":100, "name": "test name"}

#Fetch all categories
pp Category.all

#Find the categories with a specific ID
pp Category.find(1)

#Get the ID of the category with a specific ID
pp Category.find(1).id

#Get the first record of the category
pp Category.first

#Retrieve the last record of the category
pp Category.last

#get `id`,`name` of all categories
pp Category.select("id, name")

#get the number of records of `id`,`name` of all categories
pp Category.select("id, name").size

#get name = "test name" of all categories
pp Category.where(name: "test name")

#Sort name = "test" in all categories by ID and get 3 results
pp Segment.where(name: "test").order(:id).limit(3).to_json

#Sort all categories with name = "test" in ID order and get the first three.
pp Segment.where(name: "test").order(:id).limit(3).offset(1).to_json

1
2
3
4
5
6
7
8
# Return one of 4, 5, or 6 random days from the current time.
[4,5,6].sample.days.from_now
=> Wed, 07 Feb 2018 19:21:07 JST +09:00


rand(1..10).days.ago
rand(1..10).hours

Notes on how to write a FactoryGirl Blog Model Example The name and description are now sequentially numbered, and the The category is a random one of ‘programming’, ‘design’, ‘marketing’, or ‘sales’.

1
2
3
4
5
6
7
8
9
10
$ vim spec/factories/blogs.rb 

FactoryGirl.define do
  factory :blog do
    sequence(:name){|n| "name #{n}"}
    sequence(:category){ ['programming','design','marketing','sales'].sample }
    sequence(:description){|n| "description #{n}"}
    sequence(:user_id){ rand(1..3) }
  end
end

Calling the blog

1
FactoryGirl.create :blog

Handling Dates

1
2
start_datetime { rand(1..10).days.age }
end_datetime { start_datetime + rand(1..10).hours }

Here is the official statement to make it as name

1
2
User.select("settings -> 'language' as user_language")
User Load (7.1ms) SELECT settings -> 'language' as user_language FROM `users` WHERE `users`. `deleted_at` IS NULL

Get Product, categories, product_images, and DISTINCT id2,1,6 of categories

1
Product.includes(:categories, :product_images).where(categories:{id:[2,1,6]}).uniq

console Make it more visible

1
2
3
4
5
6
7
8
9
10
extend Hirb::Console

table Category.all, vertical: true

********************* 1. row *********************
            id: 1
          name: category_name
    created_at: 2018-01-15 23:16:11 +0900
    updated_at: 2018-01-15 23:16:11 +0900
********************* 2. row *********************
user-image
Akiko yokoyama in Coding
5 minute read

Similar Posts

Actually Made This Blog Multilingual Three Years Ago

Review of the Leica Q2

Re-Builded the Blog with Jekyll + Netlify + Github

Excel Tips vol.1

Setup for WSL / Windows Subsystem for Linux + Ubuntu

user-image

Published Feb 2, 2018

Akiko yokoyama in Coding

Also found in

  • Coding

Share this article

Actually Made This Blog Multilingual Three Years Ago

Review of the Leica Q2

Re-Builded the Blog with Jekyll + Netlify + Github

Excel Tips vol.1

Setup for WSL / Windows Subsystem for Linux + Ubuntu

Setting hubot adapter slack & chatwork

  • Home
  • Akiko Yokoyama
  • Contact
  • Feed