#158 Grab Bag of Ruby and Ruby on Rails Tricks


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


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.development?
  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`
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'
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
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
Issue Requests from Consoleapp.get(app.users_path)
Seeing Installed Gemsgem server

Simon Moro PRO said 4 months ago on Grab Bag of Ruby and Ruby on Rails Tricks :

Great little video of helpful tips :) Thanks!

mcfoton said 4 months ago on Grab Bag of Ruby and Ruby on Rails Tricks :

typo in clearing logs snippet: "if Rails.env.devlopment?"

kobaltz PRO said 4 months ago on Grab Bag of Ruby and Ruby on Rails Tricks :

Thank you! Correction has been made.

Login to Comment