Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Strip attributes via before_save, not just before_validation#83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
knagode wants to merge2 commits intormm5t:master
base:master
Choose a base branch
Loading
fromknagode:knagode/strip-attributes-before-save-too

Conversation

@knagode
Copy link

@knagodeknagode commentedSep 11, 2025
edited
Loading

It is often very handy to use.save(validate: false), for example when allowing users to save drafts.

I noticed (too late) that strip_attributes only strips attributes before validation. My expectation was that it would always convert'' toNULL. Because it didn’t, I ended up with many empty string values in the database, which caused unique index exceptions ('' behaves very differently fromNULL when dealing with unique indexes, and I wanted to ensure that empty strings are never persisted).

I believe strip_attributes should also work when using .save(validate: false), or at least provide a configurable setting to enable this behaviour by default across all models.

Workaround: call.valid? before.save(validate: false). This works, but it doesn’t feel intuitive.

@rmm5t
Copy link
Owner

I think I'd like to consider moving strip_attributes to use thenormalizes API, which would also solve for this. See#67.

However, this will likely require Rails 8.1 (rails/rails#53887)

In the meantime, redefiningstrip_attributes for your app and/or performing thebefore_save manually like you did is a fine workaround.

If I incorporated thisbefore_save behavior into strip_attributes to support versions prior to Rails 8.1, I'd want to make sure that the stripping normalization only occurred once per#save.

knagode reacted with thumbs up emoji

@knagode
Copy link
Author

I added code to ensure that stripping normalization isn’t executed twice. I didn’t add a test, as I don’t think it’s necessary, but I’m happy to add one if you think it’s worth it before merging. I can confirm that this PR would prevent several issues on our side but I understand that not many people use.save(validate: false).

If we were already on Rails 8.1, I’d probably just replace the strip_attributes operation with Rails’ built-in normalize.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@knagode@rmm5t

[8]ページ先頭

©2009-2025 Movatter.jp