Skip to content

Instantly share code, notes, and snippets.

@JamesYang76
JamesYang76 / coding_tips.md
Last active February 26, 2021 15:22
rails_basic

Coding tips

refer to https://legacy.gitbook.com/book/spartchou/ruby-on-rails-basic/details

Save model

Do not use @post.user_id = current_user.id, and use save! instead of save unless check return value of save

class PostsController < ApplicationController
# @post.user_id = current_user.id and 
  def create
    @post = current_user.posts.build(params[:post])
    @post.save!
@JamesYang76
JamesYang76 / aws general.md
Last active December 28, 2020 12:45
AWS general

CLI config

# make default profile
$ aws configure
$ aws configure --profile user1

$ cat ~/.aws/credentials
[default]
aws_access_key_id = AWS_KEY_ID
@JamesYang76
JamesYang76 / active_record.md
Last active April 16, 2020 02:21
ActiveRecord

supported type

:binary
:boolean
:date
:datetime
:decimal
:float
:integer

Block

def print_once
  yield
end

print_once { puts "Block is being run" }

def my_block
  yield 2
.blog-grid {
  @include xy-grid;
}

.blog-item {
  @include xy-cell(4,false,0);
}
@JamesYang76
JamesYang76 / basic.md
Created October 11, 2019 04:28
Linux basic

create ssh

$ ssh-keygen -f mykey

Webpacker

init

Change folder name from app/javascript to app/webpack

# config/webpack/webpacker.yml
# change source_path from app/javascript to app/webpack
source_path: app/webpack

Make folder named stylesheets and add application.scss into the folder\

change mode

$ rails s -e production
or 
$ RAILS_ENV=production rails s
or
$ export RAILS_ENV=production
$ printevn | grep RAILS_ENV
$ rails s

Workflow advice

When upgrading Rails versions(major or minor), we can encounter lots of unique issues.
I am writing some generic advice on how you want to tackle the update in principle.

  • Do a separate step for each major rails versions. For instance, Rails 3 to 5, there can be two steps: 3.x -> 4.2 -> 5.2
  • Do not bundle update, which can cause serious problems. Please, only update gems which have dependency errors of rails.

When we update patch(0.0.x)level, it is enough just only to update rails gem.\

@JamesYang76
JamesYang76 / Time Zone.md
Last active August 29, 2024 09:22
Time Zone for rails

Time zones

There are trhee diffrent time zones in rails

  • System - The server time zone
  • Application - Rails application use own time zone
  • Database - Default is UTC, but do not change it

Cautions

  • config.time_zone should be set when an app dose not support multiple time zones
  • Do not use Date/Time API using system time zone
  • Date could be incorrect by being converted from datetime or time to date