Comments (10)
もし、最終形がuniqueness: true
だとすると、before_save
では、バリデーションでの一意性チェックをすり抜けるケースがあると思います。
例)
- 既に
[email protected]
が、DBに登録済と仮定します。
(正常)
新規登録で[email protected]
を登録しようとすると、バリデーションでチェックされEmail has already been taken
というメッセージが表示されます。
(問題)
新規登録で[email protected]
を登録しようとすると、uniqueness: true
なので大文字/小文字が区別され、バリデーションを抜けます。
DBへのsave前に[email protected]
に変換され、セーブされるので、DBの一意性チェックでエラーになります。
RailsのエラーメッセージWe're sorry, but something went wrong.
が表示されます。
(対策) uniqueness: true
をuniqueness: { case_sensitive: false }
にするbefore_save
をbefore_validation
にする
上記のどちらかにすれば、バリデーションでチェックできるようになると思います。
(確認)
リスト6.33のテストをリスト6.26のようにduplicate_user.email = @user.email.upcase
でテストするとわかると思います。
from sample_apps.
手元で再現して同じ現象(バリデーションをすり抜けてしまうこと)を確認しました! バリデーションでキャッチできた方がユーザー視点では親切だと思うので、ご提案していただいた
uniqueness: true
をuniqueness: { case_sensitive: false }
にする
という方法で対応していきますね! 改めて、フィードバックありがとうございます...!! 🙏 ✨
from sample_apps.
@muraken-ken こちら最終的な Sample App の完成形では https://github.com/yasslab/sample_apps/blob/master/6_0_0/ch14/app/models/user.rb#L13 でできているかなと思いましたが、ココとは別の話でしょうか? 👀 💭
from sample_apps.
あー、なるほど!上記のコードを before_save
ではなく before_validation
にした方が良いのでは、というご提案ですね!把握しました!
from sample_apps.
emailのバリデーションを
uniqueness: { case_sensitive: false }
にする
@muraken-ken こちらリスト 6.27 で対応済みかなと思いました!下記コードで対応されているかなと思いますが、もし間違った認識であればご指摘いただけると嬉しいです (>人< )✨
from sample_apps.
Sample App の完成形はuniqueness: { case_sensitive: false }
なんですね。
それなら、コードとしては問題はないと思います。
私は、uniqueness: true
が完成形だと思っていました。
その理由は、以下の通りです。
リスト6.27でuniqueness: { case_sensitive: false }
に設定しています。
しかし、その後のリスト 6.32でuniqueness: true
に戻しています。
その後のリスト 6.35、リスト 6.38、リスト 6.43、リスト 8.22、リスト 9.2、リスト 9.3、リスト 9.6、リスト 9.11、リスト 10.13では、uniqueness: true
と表記されています。
なので、uniqueness: true
が完成形だと思ってました。
6章内でuniqueness: { case_sensitive: false }
に戻すことを明記し、その後のリストの表記を直していただけると間違いがなくなると思います。
また、テストもdupだけでなく、upcaseのテストも追記すると更に良いと思います。
from sample_apps.
フィードバックありがとうございます!リストのコード含め、後で見直してみますね!👀💨✨
from sample_apps.
@muraken-ken こちら確認したところ before_save
の追加と同時に uniqueness: true
に戻しているため、「新規登録するとバリデーションをすり抜け、DB登録時に一意性のエラーになる」という状態にはならなさそうでした! 一方で sample_apps 内のリポジトリでは uniqueness: { case_sensitive: false }
のままとなっていたので、こちらは後ほど修正しておきますね 🛠💨✨
フィードバックありがとうございます!(>人< )✨
from sample_apps.
なるほど...! 自分が勘違いしていたみたいですね 😂 細かな再現方法のコメントありがとうございます! (>人< )💖 手元でも確認してみます! 👀💨✨
from sample_apps.
こちらこんな感じに修正しました!(7章以降のコードも同様に修正しておきました! 👀 ✅ )
他にも何か気になる点などあれば、お気軽にご指摘いただけると嬉しいです 🙏 💖
from sample_apps.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sample_apps.