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
63
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
32
生成AIで仕事をどうにかして怠ける話
kitsunecat
0
64
懺悔LT
kitsunecat
0
47
書きながら考えないRSpec
kitsunecat
0
140
rake taskでメソッド定義したらテストが不安定になった
kitsunecat
0
120
ツクリンクで実践している 画像処理
kitsunecat
0
21
ツクリンクで実践している画像処理(フロー部分)
kitsunecat
0
240
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
430
Building Applications with DynamoDB
mza
96
7.2k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
520
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
710
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Design in an AI World
tapps
1
350
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
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は型変換頑張ってくれてた
ありがとうございました