I would like to perform a uniqueness validation on a hstore field.
When I set is as:
class User
store_accessor :attributes, :foo_attr
validates :foo_attr, uniqueness: true
end
I get undefined method 'limit' for nil:NilClass
In a Rails issue store_accessor and uniqueness validation? user al2o3cr explains:
validates_uniqueness_of is not going to work in this case - it's expecting a database column named stripe_id. With an Hstore column it's technically possible to perform the required query, but the resulting SQL is only applicable to that storage format and only works on Postgres.
In your case, a custom subclass of ActiveRecord::Validations::UniquenessValidator with an overridden build_relation would probably be a better choice.
How would you go around creating that custom validator?
I already have a database level uniqueness set up as explained in Race condition using Postgres hstore all I need now is to make valid? return false on the same foo_attr.