Reputation: 4171
We have a user model with one particular section that updates the user's mailbox which has to be unique. I added this as the validation, but it throws errors when the user updates their profile anywhere else, like password reset etc.
I am not sure how to ask, but I need to rework the if condition to only use this validation if the mailbox is not empty, and whatever else. If there is someone who has seen this type of situation and can offer any insight we would appreciate it.
validates_uniqueness_of :rss_mailbox, on: :update, if: -> user { user.rss_mailbox.presence }
validates :mailbox, exclusion: { in: :reserved_names, message: " %{value} is a reserved mailbox" }, on: :update, if: -> user { user.mailbox }
Upvotes: 0
Views: 45
Reputation: 12643
First, I've never seen the :in option used with a symbol before, so I'm not sure that it works. I don't see documentation for that, but that would not be the first "hidden feature" in Rails. I'll trust you that it works.
You really only need to run this validation when the mailbox is changed and the mailbox is present:
validates :mailbox, exclusion: { in: :reserved_names, message: " %{value} is a reserved mailbox" }, on: :update, if: -> user { user.mailbox && user.mailbox_changed? }
Upvotes: 1