How to debug the processing in the Ruby on Rails model only on the console

Hello Writing today is mainly for beginners. This time, I will introduce a certain debugging method. The debug is'byebug'. It doesn't require a gem like binding.pry, and it's attractive that view doesn't. You can debug just by writing byebug in the processing of the model or controller. However, even if you write it in view, you cannot debug it.


class Scraping < ApplicationRecord
    def self.get_infomation
        require 'mechanize'
        agent =
        links = []
        current_page = agent.get("")
        elements ='.home_talent_list_wrapper')
        boxs ='.item')
        roks ='.right')
        qqqs ='a')
        eees ='.title')
        eees.each do |eee|
            links <<  eee.inner_text
        links.each do |link|
            get_personal_infomation('' + link)
    def self.get_personal_infomation(link)
        agent =
        personal_page = agent.get(link)
        aaas ='.talent_name_wrapper')
        ages ='.age').inner_text.delete('age').to_i if'.age')
        names ='h1').inner_text  if'h1')
        image_urls ='.main_image img').get_attribute('src') if'.main_image img')
        infomation = Infomation.where(name: names).first_or_initialize
        infomation.age = ages
        infomation.image_url = image_urls

Then run get_infomation on the console

[23, 32] in /home/ec2-user/environment/filebook/app/models/scraping.rb
   23:         ages ='.age').inner_text.delete('age').to_i if'.age')
   24:         names ='h1').inner_text  if'h1')
   25:         image_urls ='.main_image img').get_attribute('src') if'.main_image img')
   26:         infomation = Infomation.where(name: names).first_or_initialize
   27:         infomation.age = ages
   28:         infomation.image_url = image_urls
   29:         byebug 
=> 30:
   31:     end
   32: end

In this way, you can stop the process in the model. You can do it by writing a method in this (byebug).

(byebug) infomation
#<Infomation id: 14, age: 22, name: "Hirose Suzu", image_url: "", created_at: "2020-11-01 07:14:44", updated_at: "2020-11-01 07:14:44">

You can take out the contents. Next, if you do it with

"Hirose Suzu"

If you make it personal_page

 {title "From the birth of Hirose Tin to the present-Talent dictionary"}
  #<Mechanize::Page::Frame nil "">}
  #<Mechanize::Page::Link "Talent dictionary" "">
  #<Mechanize::Page::Link "Talent dictionary" "">
   "Hirose Suzu"
The rest is omitted

You can extract all the information in the view.

You can do the same with the controller


def show
        @infomation = Infomation.find(params[:id])
        @comments = @infomation.comments
        @favorite = @infomation.favorites

And when you start the server

[3, 12] in /home/ec2-user/environment/filebook/app/controllers/infomations_controller.rb
    3:         @infomations = Infomation.all
    4:         @infomations_rankings = Infomation.find('count(infomation_id) desc').limit(3).pluck(:infomation_id))
    5:     end
    6:     def show
    7:         @infomation = Infomation.find(params[:id])
    8:           byebug 
=>  9:         @comments = @infomation.comments
   10:         @favorite = @infomation.favorites
   11:     end
   12: end

If you start @infomation in this state

(byebug) @infomation
#<Infomation id: 1, age: 21, name: "Kanna Hashimoto", image_url: "", created_at: nil, updated_at: nil>

And you can get the information.

Recommended Posts

How to debug the processing in the Ruby on Rails model only on the console
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to display a graph in Ruby on Rails (LazyHighChart)
[Rails / Uniqueness constraint] How to check model validation on the console / Uniqueness constraint for multiple columns
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
Things to remember and concepts in the Ruby on Rails tutorial
[Ruby on Rails] How to display error messages
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to add / remove Ruby on Rails columns
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
How to solve the local environment construction of Ruby on Rails (MAC)!
How to build the simplest blockchain in Ruby
[Ruby on Rails] How to use session method
[Ruby On Rails] How to search the contents of params using include?
How to check Rails commands in the terminal
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
How to implement image posting function using Active Storage in Ruby on Rails
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Rails] I don't know how to use the model ...
How to debug the generated jar file in Eclipse
(Ruby on Rails6) How to create models and tables
[Rails] How to display an image in the view
[Ruby on Rails] Debug (binding.pry)
[Ruby On Rails] Description that allows only specific users to transition to the edit page
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby on Rails] Only the user who posted can edit
Definitely useful! Debug code for development in Ruby on Rails
[Ruby on Rails] Quickly display the page title in the browser
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
How to retrieve the hash value in an array in Ruby
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
How to switch Java in the OpenJDK era on Mac
How to run React and Rails on the same server
Apply CSS to a specific View in Ruby on Rails
[Rails] How to display information stored in the database in view
How to deploy jQuery on Rails
[Ruby On Rails] How to search and save the data of the parent table from the child table
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to deploy Bootstrap on Rails
Ruby on Rails Japanese-English support i18n
How to install Bootstrap in Ruby
Rails on Tiles (how to write)
How to introduce jQuery in Rails 6
How to install Swiper in Rails
[Rails / Routing] How to refer to the controller in the directory you created
[Rails] How to put a crown mark on the ranking function
How to rename a model with foreign key constraints in Rails
I summarized the flow until implementing simple_calendar in Ruby on Rails.
[Ruby on Rails] Use the resources method to automatically create routes.
[Rails] How to reset the database in production environment (Capistrano version)
Measure the bottleneck! How to trace only slow methods in AspectJ