You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Devise:更新用户信息时如何跳过邮箱验证?

How to Skip Email Validation When Updating a User

Looks like your User model's email presence validation is triggering during updates, even though you bypassed it when creating the user. Here are a few practical solutions depending on your needs:

1. Restrict Email Validation to Only User Creation

The cleanest approach is to adjust your model validation so it only requires an email when creating a new user, not when updating existing ones. This enforces the rule at the model level, so you don't have to bypass validations every time you update.

In your User model:

validates :email, presence: true, on: :create

This way, the email field will only be checked for presence during the initial user creation. Updates won't trigger this validation, even if the email is blank.

2. Skip All Validations for a Specific Update (Use Cautiously)

If you need to bypass all validations entirely for a one-off update (e.g., you're only updating non-critical fields), you can use either update_columns or save(validate: false):

Using update_columns (Direct Database Update)

This method skips all validations and callbacks, updating the record directly in the database:

user.update_columns(name: "Updated Name", age: 35)

Using save(validate: false)

If you need to run callbacks but skip validations, use this approach:

user.name = "Updated Name"
user.save(validate: false)

Note: update_columns doesn't trigger callbacks like before_update, so choose based on whether those callbacks are necessary for your update.

3. Skip Only the Email Presence Validation During Updates

If you want to keep other validations active but just skip the email presence check, use a conditional validation. For example, you can require email only when creating a user or when the email field is being modified:

validates :email, presence: true, if: -> { email_changed? || !persisted? }
  • !persisted? ensures the validation runs when creating a new user (since the record doesn't exist in the database yet).
  • email_changed? ensures the validation runs if you're updating the email field (so you can't set it to blank when modifying it).

This gives you flexibility: existing users with blank emails can be updated without issues, but if you try to change their email to blank, the validation will catch it.


内容的提问来源于stack exchange,提问作者Jasjeet Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:33:11