Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
update_columnsについて無知だった話
Search
Tetsu Nishimura
May 29, 2025
58
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
update_columnsについて無知だった話
Tetsu Nishimura
May 29, 2025
More Decks by Tetsu Nishimura
See All by Tetsu Nishimura
Railsフロントエンド移行効率化 ためAエージェント利用方法
kitsunecat
0
29
生成AIで仕事をどうにかして怠ける話
kitsunecat
0
60
懺悔LT
kitsunecat
0
47
書きながら考えないRSpec
kitsunecat
0
140
rake taskでメソッド定義したらテストが不安定になった
kitsunecat
0
120
ツクリンクで実践している 画像処理
kitsunecat
0
20
ツクリンクで実践している画像処理(フロー部分)
kitsunecat
0
240
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
97
6.8k
Crafting Experiences
bethany
1
340
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
280
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.8k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
The Pragmatic Product Professional
lauravandoore
37
7.4k
Heart Work Chapter 1 - Part 1
lfama
PRO
10
36k
Designing for Timeless Needs
cassininazir
1
480
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
420
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Thoughts on Productivity
jonyablonski
76
5.4k
Transcript
© Tsukulink Inc. update_columnsについて無知だった話 #つくてくトーク
自己紹介 なまへ:にしむらてつ 趣味:スノーボード、クライミング、スキューバダイビング すんでるとこ:長野県 おしごと:ツクリンクでは非機能面の改善、フロントエンドリアーキを担当 好きな言語・技術:Rust, TypeScript, TCP/IP(Routing, Swiching,
QoS)
今日お話すること - update_columnsでDateを保存したのにnilになった話 - update_columnsで何がおきているか - 内部の型キャストの仕組みを軽くざっくり - 対処法
概要 ツクリンクDB (PostgreSQL) ツクリンクAPP Salesforce 契約情報 顧客情報など 契約情報 顧客情報など >
# DB情報を更新 > model.update_column(hoge: fuga) > > # Salesforceに同期 > salesforce.sync!(model) => 日付情報が同期されていなかった
現象再現(コード) > model = Model.find(xxx) > target_date = Date.new(2025,
5, 1) > model.update_columns(somedate_at: target_date) > # somedate_at: datetime型 > model.somedate_at => nil # 更新されたのにnilになっている このあとこのデータをつかってSalesforceにも同期をかけている
でもDBには保存されている > model = Model.find(xxx) > target_date = Date.new(2025,
5, 1) > model.update_columns(somedate_at: target_date) > model.somedate_at => nil > model.reload.somedate_at => Thu, 01 May 2025 00:00:00 JST +09:00 # DBには保存されている
キャストの問題でした ActiveRecord - update_columns は ActiveRecord の型変換をスキップしてキャッシュ -
キャスト済の値としてSQLに突っ込まれる - Date は datetime に自動変換されない - モデル内では型変換に失敗 → nil 扱いされる PostgreSQL - update_columnsから型変換されないまま更新リクエストを受ける - 各カラムについてPostgreSQLが型変換して値を更新 結果 - ActiveRecordには型変換されない値がキャッシュされる - PostgreSQLには正常に型変換されて書き込まれる
before_type_castは返ってくる > model = Model.find(xxx) > target_date = Date.new(2025,
5, 1) > model.update_columns(somedate_at: target_date) > model.somedate_at => nil > model.somedate_at_before_type_cast => Thu, 01 May 2025 # キャストしないで読み取ると値が返ってくる つまり内部的には値を持っているけど、somedate_at は nil(型変換できなかった)
Railsのコードレベルでは? - update_columns実行時 - write_cast_value 経由で(キャスト済の値として)型付き属性に直接値を書き込む - model.somedate_at実行時
- 型変換は ActiveModel::Type::DateTime で実行
どんな時に起きる? - update_columns で Date を直接保存 - saveやupdateでは型変換してキャッシュされる
- datetime カラムに対して Time ではない値を入力したとき - 読み込み時にActiveRecord がキャストできない値 => nil キャッシュ ツクリンクAPP update_columns PostgreSQL 型変換 値そのまま
対処方法 > # Timeにしてからupdate_columnsに渡す > model.update_columns( > somedate_at: target_date.to_time
> ) # または > model.update_columns( > somedate_at: Time.zone.local(2025, 5, 1) > )
まとめ - update_columns は型変換されない - update_columnsを使うときは型に注意しましょうぞ - PostgreSQLは型変換頑張ってくれてた
ありがとうございました