sharoa
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 【第22週】パRails輪読会🚂 \(2024\-01\-15\~ 2024\-01\-19\) ###### tags: `パRails🚂` - [開催概要](https://hackmd.io/4A_8ahJtQNi5hz713N5Y7w?view) - [パRails輪読会 ノートまとめ](https://hackmd.io/jsgA8Pf_RVioOgSL_VOR4g) - サンプルコード・正誤表:[サポートページ:パーフェクトRuby on Rails【増補改訂版】:|技術評論社](https://gihyo.jp/book/2020/978-4-297-11462-6/support) - **サンプルコードをダウンロードしただけでは、すぐに`rails s`できない可能性が高い**です。 以下のページに環境構築の手順をまとめたので、ドライバーをやってくださるという方は、こちらに沿って環境構築をお願いします🙏 ⏩ [環境構築の手順 2022年版](https://hackmd.io/y7qb2BRMT2Wd4tAtKYObcQ) ⏩ [環境構築の手順 2023年版](https://hackmd.io/3_lnn8_QRD6wEjwFbWcBzQ) 💁‍♀️ [更新の手順 2章編](https://hackmd.io/pZAmF2EjSmyQ38lUtJ_CqA) ## 目次 [TOC] ------ ## 2024\-01\-15(月) ### ファシリ @shodan ### ドライバー @hiromisugie ### 読んだところ 7-6-4 「バリデーションをテストする」から 8-1-2 「既存のフォームに画像アップロード機能を追加する」 383p まで。(途中です。) PR: https://github.com/PerfectRubyonRails-Rindoku/Perfect_Ruby_on_Rails_Ch6/pull/24 ### 次回 8-1-2 「既存のフォームに画像アップロード機能を追加する」 384p から。🚂 ### 学んだこと・感想 - @sharoa - 本にはバリデーションをテストする項目があったけど、実際のところはやらない?らしい。プラクティスのテストの時にまたここを思い出せたらな、と思います。 - Eventモデルに画像を紐づけるための宣言を追加する(`has_one_attached :image`)これでEventモデルでimageと言う名前の属性が使えるようになる。 - フォーム中に新しく追加した属性を受け入れてもらうためには、コントローラーの`event_params`メソッド中の`permit`メソッドに渡す引数に、追加した属性(今回だと`image``remove_image`)を許可すべきリストとして追加しないといけない。 - この何かを追加したから、ここにもそれを追加みたいな作業を忘れがちになりそうなので、気をつけたいと思う。 - @shodan - バリデーションテストをする・しないについて、この本の説明が聞けてよかったと思います。 - フォームで新しい属性を受け入れることにしたのに、受け入れ側のコントローラのストロングパラメータに追加し忘れるというよくある(?)間違いにハマれたのも、勉強という意味でとてもよかったな思います。 - なんか明日の`remove_image`まわりがややこしそう。。 - @hiromisugie - テストは面倒だけど、コード全体のメンテナンスコストを下げてくれるので、ちゃんと書くのが大切(というのはめちゃくちゃよく聞くけど、実感したことがないので、早く実感してみたい) - ActiveStorageの設定 - `bin/rails active_storage:install` - `bin/rails db:migrate` - 画像を持つモデルに`has_one_attached: image`(複数画像の場合は`has_many_attached: images`) - ビュー・コントローラの設定(ストロングパラーメータの追加は忘れがちなので注意) - @ayu-0505 - バリデーションテストについて、プラクティスのテスト技法では「必要性が薄い(もっと他にテストすべきとこあるやろ?)」的な感じでした。でもこの中で実際のやり方を見ることができて、「通常時は避けるが必要時には行う」やり方が分かってよかったです。 - Active Storage周りをちょっと忘れかけているので要復習。 - ストロングパラメーター追加忘れはプラクティスだか書籍だかでそのままやってしまってしばらくハマったような記憶。 - 今回はActive StorageのDBはデフォルトだけど、こういったサーバーまわり?は自作サービス関係で結構な鬼門になりそうで今から恐ろしい。 - @motohiro-mm - バリデーションのテスト方法もさらっと知れて良かったです - `valid?`:バリデーションが実行された結果エラーが無い場合trueを返し,エラーが発生した場合falseを返す - `assert_empty`:与えられたオブジェクトが空である場合、検査にパスしたことになる - ストロングパラメータに追加しないと許可されてないのでデータに保存されていない事が実際やってみて分かって良かったです - すぐ気づいたsuigeさんすごい! - ActiveStorageを以前やったはずなのにすっかり忘れていました… - フィヨブーフェスの時にpenoさんが「ぱRailsは辞書のように使ってる」っておっしゃってたのを思い出しました。ActiveStorageのところに戻って軽く目を通そうかなと思いました。 - @moegi29 - 今日でテストの章終わり。テストを書いてメンテナンスしていくことはそのコストを払っても得をする大きなメリットがあることになる。バグを見つける守備範囲が広い、メンテナンス容易なテストが良いテストという定義らしい。 - プロジェクトによってはバリデーションテストをする場合もある。バリデーションをテストする場合、 簡潔に書くためのマッチャーを用意してくれるgemがある。 - セキュリティの対策としてもストロングパラメータは自分がいれたもの以外は許可しない、今回は許可するのにimageとremove_imageを両方入れている ------ ## 2024\-01\-16(火) ### ファシリ @motohiro-mm ### ドライバー @hiromisugie ### 読んだところ 8-1-2 「既存のフォームに画像アップロード機能を追加する」 384p から PR: ### 次回 8-1-4 「容量が大きいファイルをアップロードした場合にバリデーションエラーにする」から。🚂 ### 学んだこと・感想 - @sharoa - remote_imageはEventモデルに対して独自に追加した属性だから、`attr_accessor`で明示的に属性を宣言する。   - [attr_accessor](https://docs.ruby-lang.org/ja/latest/method/Module/i/attr_accessor.html) - dependent :falseを追加すると、イベントが削除された時に関連するActiveStorage::Attachmentのみが削除されるが、関連するActiveStorage::Blobと画像やサムネイルは手付かずの状態になる。(削除したい時は自由なタイミングでできる。) - サムネイルを生成するの節で、初めてアクセスしたタイミングでサムネイルが作成される。リサイズした画像は設定したストレージに保存されるので、2回目のアクセス以降はサムネイル生成はされない。 - バリデーションエラーの設定をしたはずなのに、なぜかできちゃう現象、、、、明日で解決できるかな? - @moegi29 - shodanさんがはってくれたリンクで`ActiveRecord::Type::Boolean.new.cast` で文字列をBooleanに変換できることを知りました - イベントが削除されたときに関連する`ActiveStorage::Attachment`のみ削除、関連する`ActiveStorage::Blob`と画像サムネイルは手つかずの状態になるので、自由なタイミングで削除できる。表示上の画像と関連付けが削除されるということ、Ayuさんが例に挙げてくれたはてなブログの例がわかりやすかったです。 - 論理削除:実際にデータベースからは削除せず、本当は消えていないけれど、消えているのと同じ扱いにすること - ActiveStorageはファイルの中身から判断してアップロードしてくれる。テキストファイルにイメージ用の拡張子(今回はpng)をつけた場合は残念ながらはじかれない。w - @shodan - いくつかのパターンの文字列をブーリアン値に換えてくれるメソッドがある。 - `ActiveRecord::Type::Boolean.new.cast('true')` - アップロードされたファイルの関連の定義で、`dependant: false`オプションをつけると、関連元のオブジェクトが削除されたときに、その「関連」だけを削除するようにできる(元のファイルそのものや、それに紐づくテーブル上のレコードは削除されない)。 - アップロードされた画像を編集して扱いたいときは、`image_processing`gemを導入する - アップロードされる画像のバリデーションをしたいときは、`active_storage_validations`gemを導入する - 上記で通したいファイルのタイプ(`:png`とか)を指定した時、ActiveStrageはファイル名ではなくファイルの中身が本当に`png`かどうかを判断してバリデーションを行う - @motohiro-mm - ActiveStorage:PurgeJobって…? - `ActiveRecord::Type::Boolean.new.cast('true')`はブール値の`true`になる - [【Rails】文字列をBooleanに変換する方法 \(ActiveRecord::Type::Boolean\.new\.cast\) \- shungo blog](https://shungoblog.com/rails-string-to-boolean-cast/) - `has_one_attched :image, dependent: false`とすると、関連だけ削除されて表示されなくなる - `ActiveStorage::Attachment`のみ削除される、テーブル上のデータは削除されない - 今のままだと画像・サムネイル画像が残り続ける… - Rails7からはlibvipsがデフォルト設定になっている(がインストールは必要) - ActiveStorageはファイルのContentTypeをファイルの中身を見て判断しているらしい - が、textファイルをpng拡張子つけたものは通ってしまった… - [active\_storage\_validations/lib/active\_storage\_validations/content\_type\_validator\.rb at master · igorkasyanchuk/active\_storage\_validations](https://github.com/igorkasyanchuk/active_storage_validations/blob/master/lib/active_storage_validations/content_type_validator.rb) - @sadanora - `ActiveRecord::Type::Boolean.new.cast` - 引数に渡された文字列のtrue、falseなどをBool値に変換してくれるらしい - `has_one_attched :image, dependent: false` - 「イベントが削除された時に関連するActiveStorage::Attachmentのみが削除されます。」とあるけど、Railsガイドに`dependent: false`についての説明が見つけられずちょっともやっています。 - https://railsguides.jp/association_basics.html#dependent - @ayu-0505 - `remove_image`を`attr_accessor`で属性宣言し、`before_save`コールバックでtrueかどうか判断し、trueならばimage属性をnilにする処理で画像保存を削除している。 - `ActiveRecord::Type::Boolean.new.cast('文字列')`で文字列を真偽値変換できる - どのような文字列がtrue/falseになるのかは確認(大体の共通認識通り?) - vipsがRails7からは標準 - `Content Type`によるActive Storageの判断は絶対的に信頼して使うのはやめたほうがいいかも? - @hiromisugie - `dependent: false`はイベントが削除されたときに関連だけが削除されるが、ActiveStorage::Blobと画像やサムネイルは削除されない - サムネイル生成の機能、Rails7からはImageMagickではなくvipsがデフォルトになっているらしい、shodanさんありがとうございます! - 該当ページに初めてアクセスしたときにサムネイルが生成されて2回目以降はそれが使われる、というのが何気に便利だなと思った - 特定の拡張子以外のファイルがアップされたときにバリデートをかけるのは`validates :image, content_type: [:png, :jpg, :jpeg]`のように書く。 - 「テキストファイルの拡張子をpngにしてもActiveStorageは中身を見て判断してくれるからエラーになる」と書いてあったが、エラーにはならなかった…😂 - 話は逸れるけど、Content Typeまで見なくても拡張子だけでバリデートしてくれれば別にいいけどと思ったが、実際には良からぬ操作をする人もいることを想定しなきゃいけないんだなと思った ------ ## 2024\-01\-17(水) ### ファシリ @hiromisugie ### ドライバー @motohiro-mm ### 読んだところ 8-1-4 「容量が大きいファイルをアップロードした場合にバリデーションエラーにする」から 8-1-5 「縦幅や横幅が大きすぎる画像をアップロードした場合にバリデーションエラーにする」まで。 PR:https://github.com/PerfectRubyonRails-Rindoku/Perfect_Ruby_on_Rails_Ch6/pull/26 ### 次回 8-1-6 「ダイレクトアップロード時にバリデーションをしたい時の注意点」から。🚂 ### 学んだこと・感想 - @sharoa - 容量の大きいファイルや、縦横幅の大きすぎる画像のアップロードした場合にバリデーションエラーを発生させるところで、昨日content_typeを追加したvalidatesに、sizeとdimensionも追加したら、昨日の謎(本当はエラーが出ないといけないのに作成できてしまう件)が解決してしまった。また謎ができたw - とりあえず、容量が大きかったり、サイズの幅が大きすぎる時のバリデーションエラーが本通りに確認できたことはよかった。 - @moegi29 - 縦幅や横幅が大きすぎる画像からサムネイルを作ろうとするとアプリケーションサーバのメモリやディスク容量を使い果たしてしまう - validatesに`size: { less_than_or_equal_to: 10.megabytes }, dimension: { width: { max: 2000 } , height: { max: 2000 } }`というアップロード制限のコードを追記すると昨日の不具合が解決された - 今日はみんなで実験してる感じが楽しかったです - @sadanora - ActiveStorageValidationsを使うと縦横の幅や容量に制限をかけることができる。 - 「pngの拡張子に偽装したテキストファイル」が昨日は通ってしまっていたのに、一見関係なさそうな今日のコードを追記したらバリデーションが効くようになったのなぜ... - @hiromisugie - バリデーションの続き。 - 容量が大きいファイルをアップできないようにする →`size:`で設定 - 縦幅・横幅が大きすぎるファイルをアップできないようにする →`dimension: `で設定 - 上記の設定をした結果昨日の不具合(テキストファイルにpngをつけたらアップできないことを期待したがアップできてしまった)が解消され、「???」となった… - 自分が作ったファイルに問題があるのかと思いヒヤヒヤしていたのですがそうではなかった。が、むしろ謎が深まってしまった…。 - @motohiro-mm - validatesを複数書く書き方をあまりやったことがなかったので新鮮でした - 昨日通ってしまったcontent_typeのバリデーションが、容量やサイズを設定したらきちんとバリデートするようになっていた…なぜ? - `git stash`のやり方を全然わかっていないので調べようと思いました、sadanoraさんのメモも読みます、ありがとうございます〜 - @ayu-0505 - sizeの`less_than_or_equal_to`で大きい容量画像のバリデーション - dimensionの`width`と`height`で縦横の幅のバリデーション - バリデーションが中途半端に作用しなかったのはまだ謎…なんか内部の問題なのかな〜という印象 ----- ## 2024\-01\-19(金) ### ファシリ @moegi29 ### ドライバー @sugiwe ### 読んだところ 8-1-6 「ダイレクトアップロード時にバリデーションをしたい時の注意点」から 8-2-2 「Searchkickでイベント検索機能を作る」の途中まで。 PR:https://github.com/PerfectRubyonRails-Rindoku/Perfect_Ruby_on_Rails_Ch6/pull/27 ### 次回 8-2-2 「Searchkickでイベント検索機能を作る」のインストールするところから。395p。🚂 ### 学んだこと・感想 - @sharoa - ActiveStorageはダイレクトアップロードをサポートしている、と前置きしたあとで、ActiveStorageValidationsによるバリデーションには注意点がある、と書かれており、なんだかややこしいな、というのが印象です。 - 自分的にダイレクトアップロードの仕組みがよくわかっていないので、あとでshodanさんが貼ってくれたURLと合わせて調べてみたいと思います。 - ただ、相変わらずはじいて欲しいものがはじかれないという現象が直らなくて謎ですね。 - kaminariのところはなんだか落ち着いて見ることができましたw - @motohiro-mm - ダイレクトアップロード:ブラウザからRailsアプリでデータを受け取ることなく直接クラウドストレージにアップロードできる - [ActiveStorageダイレクトアップロードでファイルサイズの制限を設けるには](https://zenn.dev/takeyuwebinc/articles/019b7a825cd5b8) - ダイレクトアップロードでは不完全なバリデーションしかできない(らしい) - kaminariはなんだか懐かしかったです - 検索機能は、検索対象のレコード数が少なければActiveRecordのクエリメソッドでできる - レコード数が多かったり、仕様が複雑なときは他の検索エンジンを使う(今回はElasticsearch) - @shodan - ダイレクトアップロードは、Railsのアプリケーションサーバを通さず、クライアント側からクラウドストレージに直接ファイルの実態を保存する方法 - このとき、通常のバリデーションが同じように機能しないことがあることに注意 - もしかしたらなんですが、Elasticsearchのバージョンが本の時から進んで、インストールするためのコマンドが変わったかも? - 最新のインストールガイドページ https://www.elastic.co/guide/en/elasticsearch/reference/current/targz.html - 一つ前のバージョンのインストールガイド https://www.elastic.co/guide/en/elasticsearch/reference/7.17/targz.html - ↑ここで本で紹介しているコマンドが載っていて、最新のコマンドは全然ちがいますね。 - @sugiwe - ActiveStorageはちょっと鬼門かもと思った…!自作サービスやその先で画像アップロードを扱うことになったら気をつけたい。 - kaminariはプラクティスの復習にもなってよかった! - kaminariの表示をいい感じにするbootstrap用のビューテンプレートが用意されていることを知った - 検索機能は結構ややこしくて、Elasticsearchなどの全文検索エンジンを利用すると実装しやすいらしい -----

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully