#158 Grab Bag of Ruby and Ruby on Rails Tricks

Summary

In this episode, we look at various tips and tricks.
development rails model ruby json dev 12:35

Summary

Terminalrails new APP_NAME
rails g scaffold users first_name last_name
Rails Notes# TODO some todo message
# FIXME some fixme message
# OPTIMIZE some optimize message

rails notes
Clear Development Logs# config/initializers/clear_logs.rb

if Rails.env.devlopment?
  MAX_LOG_SIZE = 10.megabytes
  logs = File.join(Rails.root, 'log', '*.log')
  if Dir[logs].any? { |log| File.size?(log).to_i > MAX_LOG_SIZE }
    $stdout.puts 'Running rails log:clear'
    `rails log:clear`
  end
end
Sandboxing Rails Consolerails c --sandbox
Setting Email From Name# app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: 'Example Application <[email protected]>'
  layout 'mailer'
end
Using dig and fetch on Hashesuser = {
  user: {
    attributes: {
      first_name: 'john',
      last_name: 'doe'
    },
    address: {
      street: '123 fake street',
      city: 'atlanta',
      state: 'georgia',
      postal_code: '12345',
      location: {
        latitude: 33.748527, 
        longitude: -84.386635
      }
    }
  }
}

user[:user] && user[:user][:address] && user[:user][:address][:street]

user.dig(:user, :address, :street)
user.dig(:user, :address).fetch(:street)
puts user.dig(:user, :address).fetch(:postal_code, 'None Provided')
Delegating Methods to Another Class# Terminal

rails g model profile user:references
rails db:migrate

# app/models/profile.rb

class Profile < ApplicationRecord
  belongs_to :user
  # Profile.first.user.first_name
  # Profile.first.first_name
  delegate :first_name, to: :user
end
Array Setters, Union and Subtraction2.4.3 :001 > ing1, ing2 = ['salt', 'pepper']

2.4.3 :002 > [1, 2, 3] & [3, 4]
 => [3]

2.4.3 :003 > [1, 2, 3] - [2, 3]
 => [1]
Ruby Method Source Location and Displayuser = User.first
user.fullName
user.method(:fullName).source_location
user.method(:fullName).source
user.method(:fullName).source.display
Issue Requests from Consoleapp.get(app.users_path)
app.cookies
Seeing Installed Gemsgem server


Simon Moro PRO said 4 days ago:

Great little video of helpful tips :) Thanks!

Login to Comment