Note To Self ~One Step At A Time~

AUTHOR
kkawazoe
162 Posts
62 Tags

ページ内検索

Rails における Enum について

  • Dec 2, 2022
  • POST
Rails における Enum の挙動について調査した内容を備忘録として残しておく 環境 ruby: 2.7.6 rails: 5.2.8.1 実際のコード DB のカラムに対応する enum を定義した場合、自動的に scope が定義される ※Rails 6 では not_* の scope も自動的に定義される 参考 ※Rails 7 では新しい構文が追加されたため、今後オプションの指定が従来のやり方だと非推奨になる可能性がある 参考 [モデル定義] class Article < ApplicationRecord enum status: { draft: 0, # 下書き published: 1, # 公開中 closed: 2, # 掲載終了 }, _prefix: true end [Rails Console] # enum 確認 Article.statuses #=> {"draft"=>0, "published"=>1, "closed"=>2} # scope Article.status_published #=> Article Load (7.

Ruby Gold 認定試験 Ver 3 への道 その4

  • Dec 1, 2022
  • POST
Ruby Gold 認定試験の Ver 3 の勉強時に学んだことを備忘録として残しておく [前回までの記事] Ruby Gold 認定試験 Ver 3 への道 Ruby Gold 認定試験 Ver 3 への道 その2 Ruby Gold 認定試験 Ver 3 への道 その3 環境 ruby: 3.1.2 キーワード引数 キーワード引数で a: のようなデフォルト値を指定しない場合、構文エラーにはならないが、呼び出し時に指定しなかった場合に ArgumentError になる class KeywordArgument def foo(a:, b: 'bbb') puts "a: #{a}, b: #{b}" end end keyword_argument = KeywordArgument.new keyword_argument.foo(a: 'aaa', b: 'BBB') #=> a: aaa, b: BBB keyword_argument.foo(a: 'aaa') #=> a: aaa, b: bbb keyword_argument.

Ruby Gold 認定試験 Ver 3 への道 その3

  • Nov 25, 2022
  • POST
Ruby Gold 認定試験の Ver 3 の勉強時に学んだことを備忘録として残しておく [前回までの記事] Ruby Gold 認定試験 Ver 3 への道 Ruby Gold 認定試験 Ver 3 への道 その2 環境 ruby: 3.1.2 public_send メソッド Object#public_send メソッドは private メソッドを呼び出せない private_send メソッドは存在しない ※private メソッドを 呼び出す場合は Object#send or BasicObject#__send__ メソッドを使用する class PublicTest def public_method puts 'call public methods' end private def private_method puts 'call private methods' end end test = PublicTest.new test.public_method test.private_method #=> call public methods #=> public_send.

Ruby Gold 認定試験 Ver 3 への道 その2

  • Nov 24, 2022
  • POST
Ruby Gold 認定試験の Ver 3 の勉強時に学んだことを備忘録として残しておく [前回までの記事] Ruby Gold 認定試験 Ver 3 への道 環境 ruby: 3.1.2 __FILE__ __FILE__ が書いてあるファイルのファイル名を表す 似たようなので $0 があるがこちらは直接実行したファイルのファイル名を表す puts __FILE__ puts $0 require_relative './file_name' $ ruby file_name.rb __FILE__: file_name.rb $0: file_name.rb $ ruby require_file_name.rb __FILE__: file_name.rb $0: require_file_name.rb 累乗の計算 def exponentiation(n) n ** n end puts exponentiation(-1) #=> -1 puts exponentiation(1i) #=> 0.20787957635076193+0.0i 複素数 正規表現 =~ の $1 等 'www.ruby.co.jp'.match(%r(www(\.ruby\.co)(\.jp))) do |e| puts "match e= #{e}" puts "match e[0]= #{e[0]}" puts "match e[1]= #{e[1]}" puts "match e[2]= #{e[2]}" puts "match $0 #{$0}" puts "match $& #{$&}" puts "match $1 #{$1}" puts "match $2 #{$2}" end #=> match e= www.

Ruby Gold 認定試験 Ver 3 への道

  • Nov 23, 2022
  • POST
Ruby Gold 認定試験の Ver 3 の勉強時に学んだことを備忘録として残しておく 環境 ruby: 3.1.2 Numbered parameters _1 から始まる _1, _2 で 第一引数, 第二引数の意味となる 以下は同じ意味 h = { a: 1, b: 2, c: 3 } h.transform_values{|v| v * 2} #=> {:a=>2, :b=>4, :c=>6} h.transform_values{_1 * 2} #=> {:a=>2, :b=>4, :c=>6} h = { a: 1, b: 2, c: 3 } h.map{|key, value| [key, value * 2]}.to_h #=> {:a=>2, :b=>4, :c=>6} h = { a: 1, b: 2, c: 3 } h.

Visual Studio Code で拡張子毎に設定値を変更する方法

  • Nov 22, 2022
  • POST
経緯 Visual Studio Code にて保存時の空白のトリミングの挙動を markdown の場合のみ変えたくなったため調査を行なった その結果を備忘録として残しておく 手順 コマンドパレットを開く Configure language specific settings を入力して選択する 拡張子を質問されるので markdown を選択する settings.json ファイルに以下のように編集する 実際の設定ファイル { "[markdown]": { "files.trimTrailingWhitespace": false } }

rails console で オートコンプリートを無効にする方法

  • Nov 22, 2022
  • POST
ruby 3.1 以降から irb でオートコンプリートが効くようになったがそれを無効にする方法を調査した その結果を備忘録として残しておく 環境 ruby: 3.1.2 rails: 6.1.7 方法 カレントディレクトに .irbrc ファイルを作成して以下のように編集する ※他の設定値については 参考 を参照 IRB.conf[:USE_AUTOCOMPLETE] = false IRB.conf[:SAVE_HISTORY] = nil

rails で中間テーブルをチェックボックス複数選択させて生成する方法

  • Nov 3, 2022
  • POST
環境 ruby: 2.7.6 rails: 5.2.8.1 前提 rails で has_many の ids を使用して中間テーブルの関連付けを行う方法 の構成を参照 実際のコード erb で行うパターンと javascript で行うパターンはそれぞれ以下 ※基本的には erb で行うと思うが、 erb の構成に対して値を受け渡す場合にjavascript のパターンを使用する erb のパターン association を使用して as: :check_boxes オプションを指定する <%= simple_form_for @book do |f| %> <%= f.association :categories, as: :check_boxes %> <%= f.button :submit %> <% end %> javascript のパターン 最終的に生成される params が以下のようになるように組み立てる ["", 1, 3] $('form').append( `<input type="hidden" value="" name="book[category_ids][]" />`, ); book.

rails で has_many の ids を使用して中間テーブルの関連付けを行う方法

  • Nov 1, 2022
  • POST
環境 ruby: 2.7.6 rails: 5.2.8.1 実際のコード [ER 図] ER 図 [model] class Book has_many :book_categories, dependent: :destroy has_many :categories, through: :book_categories end class BookCategory belongs_to :book belongs_to :category end class Category has_many :book_categories has_many :books, through: :book_categories end [controller] category_ids を配列形式で許可する def create @book = Book.save!(book_params) end private def book_params params.require(:book).permit( :title, category_ids: [] ) end

ransack で scope を使用して link_to で表示する方法

  • Nov 1, 2022
  • POST
方法 ransackable_scopes メソッドを使用して実現する 環境 ruby: 2.7.6 rails: 5.2.8.1 実際のコード 例. User モデルの created_at カラムを基準として N 以内に作成されたユーザを取得する [model] # 引数なし scope :within_one_month, -> { where('created_at >=', Time.zone.now - 1.months) } # 引数あり scope :within_days, -> (day) { where('created_at >=', Time.zone.now - (day).days) } def self.ransackable_scopes(_auth_object = nil) %i(within_days within_one_month) end [erb] <%= link_to( "1ヶ月以内に作成されたユーザ一覧", users_path(@q, q: { within_one_month: true }), class: "btn btn-primary mr-5" ) link_to( "7日以内に作成されたユーザ一覧", users_path(@q, q: { within_days: 7 }), class: "btn btn-primary mr-5" ) %> [controller]
  • ««
  • «
  • 8
  • 9
  • 10
  • 11
  • 12
  • »
  • »»
LATESTS
pgAdmin4 でソフトウェア自体の更新する際にエラーが発生する場合の対処法
  • Feb 6, 2026
  • POST
Google スプレッドシートにて大量の行数が存在するシートに対して連番を付与する方法
  • Jan 26, 2026
  • POST
WSL 上の VSCode で 'Exec format error' が発生して起動しなくなったときの対処方法
  • Jan 17, 2026
  • POST
JSTQB Foundation Level への道 その2
  • Nov 20, 2025
  • POST
JSTQB Foundation Level への道
  • Sep 19, 2025
  • POST
Rails 8.1 リリースノートまとめ
  • Sep 9, 2025
  • POST
RSpec で Cookie のテストを行う方法
  • Jun 19, 2025
  • POST
Serverless 構成で lambda ローカルデバッグ用に AWS SAM CLI を使用する
  • May 16, 2025
  • POST
Rails 8 で docker を使用している場合に rspec > requests spec で 403 エラーが発生した場合の確認点について
  • May 8, 2025
  • POST
Rails 8 rails new コマンドのオプションについて
  • Apr 14, 2025
  • POST
TAGS
  • ruby-on-rails (43)
  • tools (41)
  • aws (31)
  • 資格 (17)
  • github (8)
  • postgresql (8)
  • vs-code (5)
  • amazon-rds (4)
  • rspec (4)
  • vue.js (4)
ABOUT
プライバシーポリシー
  • Mar 1, 2019
  • ABOUT
マインドマップ

© Note To Self ~One Step At A Time~

Powered by Hugo.

Robust designed by Daisuke Tsuji.