Rails

Rails collectionのpertialでindexを取得する

Railsのパーシャル内で現在のインデックス数を取得する方法です。 パーシャル内で #{variable_name}_counter で取得可能です。 = render partial: 'users/user', collection: @users index : <%= user_counter %>

Rspecで WARNING: Using `expect { }.not_to raise_error(SpecificErrorClass)` risks false positives

Rspec実行時に以下で怒られました。 WARNING: Using `expect { }.not_to raise_error(SpecificErrorClass)` risks false positives, since literally any other error would cause the expectation to pass, including those raised by Ruby (e.g. `NoMethod…

Don't know how to build task 'active_storage:install'

rails active_storage:install をしようとしたら以下のエラー Don't know how to build task 'active_storage:install' config/application.rb で以下をコメントアウトしているのが原因でした # require "active_storage/engine"

Your bundle is locked to mimemagic (0.3.5), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of mimemagic (0.3.5) has removed it. You'll need to update your bundle to a

Railsプロジェクトを動かそうとしたら下記のエラー Your bundle is locked to mimemagic (0.3.5), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of mimemagi…

よく使うRails 6.1で生成ファイルを最小限にするrails newコマンド

環境 Ruby 2.7.2 Rails 6.1.0 最小限の rails new rails new --minimal -S -J -T --database=postgresql TestApp --minimal 以下のフレームワークがスキップされる action_cable action_mailbox action_mailer action_text active_job active_storage bootsn…

Rails Searchkickで circuit_breaking_exception Data too largeエラー 分割インデックスする方法

RailsアプリでSearchkickを使いElasticsearchのreindexをしようとしたところデータ量が大きすぎるとのエラー Elasticsearch::Transport::Transport::ServerError · [429] {"error":{"root_cause":[{"type":"circuit_breaking_exception","reason":"[parent] …

Rails ActiveRecordで特定のカラムのユニークな配列を取得する

例えば created_on に日付が入っているこんなデータ Post.last Post Load (13.1ms) SELECT "posts".* FROM "posts" => #<Post id: 1, user_id: 1, created_on: "2020-11-02"> pluck を使うだけだと同じ日付が入ってしまう Post.pluck(:created_on) (2.7ms) SELECT "posts"."created_on" FROM "posts" => [Mon, 0</post>…

Rails test環境のDBを初期化する

RAILS_ENV で test を指定する bundle exec rails db:migrate:reset RAILS_ENV=test 削除、作成、migrateを個別にする bundle exec rails db:drop RAILS_ENV=test bundle exec rails db:create RAILS_ENV=test bundle exec rails db:migrate RAILS_ENV=test

Rubyのmin_byで比較対象にnilがいるとArgumentErrorでコケる

Rubyの簡単なサンプル arr = [ { name: "hoge1", age: 10 }, { name: "hoge2", age: 13 }, { name: "hoge3", age: 15 }, { name: "hoge4", age: nil } ] arr.min_by{ |e| e[:age] } => ArgumentError (comparison of NilClass with 10 failed) ブロックの戻…

RailsでEnum元の数値を取得する

以下の設定の場合 class User < ActiveRecord::Base enum role: { admin: 1, viewer: 2, other: 3 } ... Rails5の場合 user = User.create(role: 1) user.role_before_type_cast # => 1 # もしくは user.class.roles[user.role] # => 1 Rails4以前は user = …

libreadlineのエラーでrails consoleが動かない問題

解決法 とりあえず解決した方法。 無いぞと言われてるパスにシンボリックリンクを張る ln -s /usr/local/opt/readline/lib/libreadline.dylib /usr/local/opt/readline/lib/libreadline.6.dylib 出たエラー /Users/user/.rbenv/versions/2.4.0/lib/ruby/2.4.…

Rails RansackでNULLS LASTでsortする

Ransackのconditionには入れられないらしい NULLS LASTでsortするには Ransackの外側で普通にOrder指定する Model.search(conditions).result.order('column DESC NULLS LAST') 脱Ransackしたい 参考 How to add NULL LAST to sorts · Issue #443 · activere…

RailsでPostgreSQLにNULLS LASTのインデックスを追加する方法

タイトルママ nulls lastのインデックスを作りたいけどなかなか見つからなかったので書きました。 migration にこれ add_index :table, :column, order: { column: 'DESC NULLS LAST' } 参考 PostgreSQL: Ordering, NULLs, and indexes - makandropedia 元の…

content_tagで閉じがないhtmlタグ<br/>や<img/>を出力する?

<br /> を出力したくて content_tag(:br) とやったら <br></br> と出力されてしまった… content_tagじゃなくてtagを使うとのこと なるほどcontentがあるかないかの単純な話だったらしい tag知らなかった… 結果 無事brタグが出力されました tag(:br) ↓ <br />