Libraries

最新コメント

rails 7.0からrails 7.1への移行(アップデート、アップグレード)で必要な作業

wakairo @wakairo
最終更新

Rails 7.1でのaction_dispatch.show_exceptionsの変更への対応方法

対応の基本的な方向性

config.action_dispatch.show_exceptionsを今後のデフォルトとなる:rescuableに設定してbin/rails testでテストを実行します。テストの実行に問題が無く、:rescuableの仕様で支障が無い場合はこれでOKです。

設定で:rescuableを選択してテストを実行するとエラーが出るようになってしまった場合は、テストを修正するか、:rescuable以外の設定値にします。元々trueと設定していた場合には:allfalseと設定していた場合には:noneへと設定し直せば、以前の挙動と同じになり、エラーは出なくなるはずです。

より詳しい情報を紹介している記事

本件の背景やテストの修正方法など、より詳しい情報はこちらの記事をご覧下さい。

0
Raw
https://www.techtips.page/ja/comments/223

rails 7.0からrails 7.1への移行(アップデート、アップグレード)で必要な作業

wakairo @wakairo

Rails 7.1への移行におけるActive Storageのmigration

bin/rails app:updateコマンドを実行すると以下の3つのファイルがdb/migrate/に作成されますが、
Active Storageをまだ使用したことがないアプリケーションでは、 これら3つのファイルは単純に削除してしまって良いはずです。

  • xxx_add_service_name_to_active_storage_blobs.active_storage.rb
  • xxx_create_active_storage_variant_records.active_storage.rb
  • xxx_remove_not_null_on_active_storage_blobs_checksum.active_storage.rb

より正確に言うと、データベースにactive_storage_blobsテーブルが存在していない場合は、 これら3つのファイルは単純に削除してしまって良いはずです。 その理由として、これら3つのファイルはデータベースのactive_storage_blobsテーブルに対して影響を与えるものであり、 table_exists?(:active_storage_blobs)がfalseを返すと何もせずにreturnする処理が3つのファイル全てに記述されているからです。

ご自身のRailsアプリケーションのデータベースにactive_storage_blobsテーブルが存在するかの確認はbin/rails consoleを実行して、 以下のようにコマンドを実行することで行えます。 結果がfalseであればactive_storage_blobsテーブルは存在していない、つまり前述の3つのファイルは削除で大丈夫のはずです。

irb(main):002> ActiveRecord::Base.connection.table_exists? :active_storage_blobs
=> false

ちなみに、データベースにあるテーブルの一覧を見たい場合にはbin/rails consoleActiveRecord::Base.connection.tablesを叩くことでテーブルの一覧を確認できます。

0
Raw
https://www.techtips.page/ja/comments/222

rails 7.0からrails 7.1への移行(アップデート、アップグレード)で必要な作業

wakairo @wakairo

基本的にはRailsガイドの手順に従えば良いと思います。

ガイドの手順にもありますが、ぜひbin/rails app:updateコマンドを活用しましょう。

bin/rails app:updateコマンドの実行例

# bin/rails app:update
   identical  config/boot.rb
       exist  config
    conflict  config/application.rb
Overwrite /x/techtips/config/application.rb? (enter "h" for help) [Ynaqdhm] h
        Y - yes, overwrite
        n - no, do not overwrite
        a - all, overwrite this and all others
        q - quit, abort
        h - help, show this help
        d - diff, show the differences between the old and the new
        m - merge, run merge tool
Overwrite /x/techtips/config/application.rb? (enter "h" for help) [Ynaqdhm]
       force  config/application.rb
   identical  config/environment.rb
       exist  config/environments
    conflict  config/environments/development.rb
Overwrite /x/techtips/config/environments/development.rb? (enter "h" for help) [Ynaqdhm]
       force  config/environments/development.rb
    conflict  config/environments/production.rb
Overwrite /x/techtips/config/environments/production.rb? (enter "h" for help) [Ynaqdhm]
       force  config/environments/production.rb
    conflict  config/environments/test.rb
Overwrite /x/techtips/config/environments/test.rb? (enter "h" for help) [Ynaqdhm]
       force  config/environments/test.rb
       exist  config/initializers
    conflict  config/initializers/assets.rb
Overwrite /x/techtips/config/initializers/assets.rb? (enter "h" for help) [Ynaqdhm]
       force  config/initializers/assets.rb
    conflict  config/initializers/content_security_policy.rb
Overwrite /x/techtips/config/initializers/content_security_policy.rb? (enter "h" for help) [Ynaqdhm]
       force  config/initializers/content_security_policy.rb
      create  config/initializers/cors.rb
    conflict  config/initializers/filter_parameter_logging.rb
Overwrite /x/techtips/config/initializers/filter_parameter_logging.rb? (enter "h" for help) [Ynaqdhm]
       force  config/initializers/filter_parameter_logging.rb
   identical  config/initializers/inflections.rb
      create  config/initializers/new_framework_defaults_7_1.rb
    conflict  config/initializers/permissions_policy.rb
Overwrite /x/techtips/config/initializers/permissions_policy.rb? (enter "h" for help) [Ynaqdhm]
       force  config/initializers/permissions_policy.rb
      remove  config/initializers/cors.rb
       exist  bin
   identical  bin/rails
   identical  bin/rake
    conflict  bin/setup
Overwrite /x/techtips/bin/setup? (enter "h" for help) [Ynaqdhm]
       force  bin/setup
       rails  active_storage:update
Copied migration 20231128024504_add_service_name_to_active_storage_blobs.active_storage.rb from active_storage
Copied migration 20231128024505_create_active_storage_variant_records.active_storage.rb from active_storage
Copied migration 20231128024506_remove_not_null_on_active_storage_blobs_checksum.active_storage.rb from active_storage

After this, check Rails upgrade guide at https://guides.rubyonrails.org/upgrading_ruby_on_rails.html for more details about upgrading your app.
0
Raw
https://www.techtips.page/ja/comments/213

closure_treeはRailsのfixturesには未対応の模様

wakairo @wakairo

closure_treeは、現在のところfixturesと組み合わせて利用されることが想定されていないようです。
と言うのも、closure_tree gemを利用したRails 7のappのtestにおいて、fixturesを用いているのですが、いくつか困った現象に遭遇しているためです。

開発で困っている現象

具体的には、以下のような現象に遭遇しています。

現在の対応策

これらの現象は、test中でcreateメソッド等を使用して作成したオブジェクトでは発生しません。
そこで前述の現象に対応するため、closure_treeを利用しているモデルでは、create等のメソッドでオブジェクトを作成し、testに用いています。
なお、小手先のテクニックとして、fixturesで記述したテスト用初期設定を、createで作ったオブジェクトに移し替える処理を書くという方法があります。こうすることで、テスト用初期設定はfixturesに書きつつも、困った現象は回避することが出来ます。

0
Raw
https://www.techtips.page/ja/comments/207

Stimulusの記事の情報が集まるTopic

wakairo @wakairo
最終更新

Stimulusには、Targetに、Valueに、Action Parameterにと、HTML側からJavaScript側へ何かしらを引き渡す仕組みがいろいろとあります。ですので、これら3つの仕組みについてサンプルコードを交えて説明する記事を書きました。

0
Raw
https://www.techtips.page/ja/comments/206

Stimulusの記事の情報が集まるTopic

wakairo @wakairo

公式ドキュメント(英語)

公式情報がしっかりしているので、まず読むならこれら公式情報でしょうが、英語なのが日本人にはちょっと大変。

0
Raw
https://www.techtips.page/ja/comments/205
😄8

AWS Amplifyを使ったSlackbotの開発

YamaKawa @YamaKawa
最終更新

※書き途中の内容です。

問題

Amplifyで作成したプロジェクトでamplify pushを使ってデプロイしたLambdaを実行すると[ERROR] Runtime.ImportModuleError: Unable to import module 'index': urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fipsというエラーが発生する。

そもそも実現したいこと

LambdaでLangChainを使ってPineconeからフロントで入力された質問に対する回答をフロントに返したい。

行った手順

amplify pushでLambdaをデプロイしたところ、[ERROR] Runtime.ImportModuleError: Unable to import module 'index': urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fipsというエラーが発生

解決策

OpenSSLのバージョンを1.1.1+に変更

次に発生したエラー

[ERROR] Runtime.ImportModuleError: Unable to import module 'index': urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips

→ pip install urllib3で解決


次に発生したエラー

[ERROR] Runtime.ImportModuleError: Unable to import module ‘index’:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
    https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
  * The Python version is: Python3.8 from “/var/lang/bin/python3.8”
  * The NumPy version is: “1.24.4"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named ‘numpy.core._multiarray_umath’
Traceback (most recent call last):

→ 多分NumPyは1.24.4を入れて解決

別に発生したエラー

The following resources failed to deploy:
Resource Name: LambdaFunction (AWS::Lambda::Function)
Event Type: update
Reason: Resource handler returned message: "Unzipped size must be smaller than 135390475 bytes (Service: Lambda, Status Code: 400, Request ID: a3b51e9a-4734-435e-a001-2c7ce1301ee7)" (RequestToken: 92a2a218-8b38-6b12-baff-133e7d710537, HandlerErrorCode: InvalidRequest)
URL: https://console.aws.amazon.com/cloudformation/home?region=ap-northeast-1#/stacks/arn%3Aaws%3Acloudformation%3Aap-northeast-1%3A237156174022%3Astack%2Famplify-slackbottesttwo-dev-162215-functionslackLangchainResponder-1PUTGEZ3KV53F%2F6707b020-344c-11ee-a124-0e162f03cf95/events

→以下の手順で解決

  1. pipenvでインストールしたパッケージを記録
  2. pipenvでインストールしたパッケージをアンインストール
  3. 1.で記録したパッケージを元にLambdaレイヤー作成

■レイヤーの作り方

  1. Lambdaで「レイヤー」選択
  2. レイヤーの作成
  3. 対象zipアップロード
  4. x84_64選択
  5. ランタイム:python3.8選択
  6. 対象関数で「レイヤーの追加」ボタン押下
  7. カスタムレイヤーで作ったレイヤー選択
0
Raw
https://www.techtips.page/ja/comments/203
😄1

rails 7.0.4 で pg 1.5 を使うと警告が出る問題

wakairo @wakairo
最終更新

昨日rails 7.0.5がリリースされました。
早速試したところ、rails 7.0.5とpg 1.5の組み合わせでは、前述の警告が出ないことを確認できました。
そこで、この結果を踏まえて、このTopicの先頭コメントの記述を更新しました。

0
Raw
https://www.techtips.page/ja/comments/200

rails 7.0.4 で pg 1.5 を使うと警告が出る問題

wakairo @wakairo
最終更新

結論

railsのバージョンを7.0.5以上に上げてから、pg 1.5 にバージョンを上げれば、警告の問題は発生しません。

問題が発生したバージョン

  • ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
  • rails (7.0.4.3)

問題を引き起こした作業内容

bundle updateでpgのバージョンを1.4系から1.5系に上げた。

発生した問題

rails testで以下のような警告が複数出るようになった。

PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from /usr/local/bundle/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/postgresql_adapter.rb:980:in `new'

rails 7.0.5のリリース後に行った対処

Gemfileにて、pgは1.5以降、railsは7.0.5以降を使うように指定してから、bundle updateコマンドを実行した。
その後でrails testを実行し、前述の警告が出ないことを確認した。

rails 7.0.5のリリース前に行った対処

Gemfileに以下のように記述して、利用するpgを1.4系に留めるように指定した。

gem "pg", "~> 1.4.6"

rails本家の対応状況

0
Raw
https://www.techtips.page/ja/comments/164

bundleコマンドの公式マニュアル

wakairo @wakairo
最終更新

bundleコマンドの公式マニュアルは、公式サイトから、 Docs > Reference Guidesで辿ることができる。

例えば、bundle updateのマニュアルはこちら。こちらでは、利用できるオプションについてもきちんと説明されています。

ちなみに、Google検索からでは、なぜか公式マニュアルにたどり着きにくい気がするので、このTopicを立てました。

0
Raw
https://www.techtips.page/ja/comments/163