paranoia_uniqueness_validator icon indicating copy to clipboard operation
paranoia_uniqueness_validator copied to clipboard

Specifying the column name?

Open dhempy opened this issue 7 years ago • 6 comments
trafficstars

I've use an external database (schema is not under my control) that has a field , "deleted_timestamp" instead of "deleted_at".

acts_as_paranoid accepts a column name, like this:

acts_as_paranoid column: :account_deleted_ts

Is there any way to tell this validator to use a different column?

dhempy avatar Apr 26 '18 20:04 dhempy

ps. I'm using paranoia_uniqueness_validator ~> 0.1.0 for my crusty old Rails 3.2 app. (shame)

dhempy avatar Apr 26 '18 20:04 dhempy

Unfortunately, no. I would love to support that though. A PR would be welcome.

anthonator avatar Apr 26 '18 20:04 anthonator

I'm game to take that on. Do you have a preference for what the interface might look like?

dhempy avatar Apr 26 '18 20:04 dhempy

I would say add a column option to the validator. So it would look something like this.

validates :field, uniqueness_without_deleted: true, column: :my_column

What do you think?

anthonator avatar Apr 26 '18 20:04 anthonator

Looks reasonable. I'll take a stab at that next week.

dhempy avatar Apr 26 '18 20:04 dhempy

And in case I don't get around to it, and someone else needs this, here's a close workaround:

validates :user_name, uniqueness: { scope: :deleted_timestamp }

I think this is not quite perfect, as I believe it would not allow two users with the same user_name to be deleted at the same timestamp. But, that's not a real-world problem for me, and might not be for you.

dhempy avatar Apr 26 '18 20:04 dhempy