Skip to content

Instantly share code, notes, and snippets.

View danilovelozo's full-sized avatar
📚
Always learning

Danilo Velozo danilovelozo

📚
Always learning
View GitHub Profile
@danilovelozo
danilovelozo / application_record.rb
Last active December 15, 2022 14:42
Battling RecordNotUnique in Rails
class ApplicationRecord < ActiveRecord::Base
attr_writer :created_moments_ago
def created_moments_ago?
@created_moments_ago
end
def self.create_or_take!
where(block_given? && yield).crumby_create!
rescue ActiveRecord::RecordNotUnique
@danilovelozo
danilovelozo / equipment.rb
Last active January 17, 2021 14:21
Rails ActiveRecord Scopes
# app/models/equipment.rb
class Equipment < ApplicationRecord
has_many :requests
has_many :customers, through: :requests
validates :brand, presence: true
validates :model, presence: true
validates :equipment_type, presence: true
validates :serial_no, presence: true
@danilovelozo
danilovelozo / commands.bash
Last active January 17, 2021 12:33
Delegate Pattern With Rails
rails generate model User first_name:string last_name:string email:string
rails generate Profile tagline user:references
rails db:migrate
User.create({name: 'Danilo Velozo', email: 'velozo.dan@gmail.com', password: '12345', password_confirmation: '12345'})
Profile.create(tagline: 'My Awesome Profile', user_id: User.first.id)
# Or