Questions tagged [validates-uniqueness-of]

validates_uniqueness_of is a rails helper method for determining whether an attribute is unique throughout a system

In Ruby on Rails, the validates_uniqueness_of method determines whether an attribute is unique across a system.

http://apidock.com/rails/v4.2.1/ActiveRecord/Validations/ClassMethods/validates_uniqueness_of

94 questions
1
vote
2 answers

Rails validate uniqueness of attribute according to another table

I have a model User, which has_many Profile. I also have Report model, which belongs_to Profile. How can I make sure that one user has only one report? Something like class Report validate_uniqueness_of profile_id, scope: :user end would be…
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
1
vote
0 answers

custom uniqueness validation with rails

I am trying to make a custom validator for uniqueness. The following example does not work for some reason: validates :language_id, uniqueness: { scope: :country_id}, if: :no_detail? validates :language_id, uniqueness: { scope:…
Alex C
  • 1,334
  • 2
  • 18
  • 41
1
vote
2 answers

Rails: Validates uniqueness of scoped by date

I have a Rails model that should only allow saving/updating of a model once per day per user. I have a callback to do the Find by user and date then add to errors but this is ugly and feels un-rails-like. I have the typical created_at / updated_at…
Matt Rogish
  • 24,435
  • 11
  • 76
  • 92
1
vote
1 answer

How do I validate that elements of a group (collection) are unique per parent object in Rails?

My app calls for Courses with Projects with Groups with Users. I need to make sure that users never appear more than once per project and I think I can do it with a Rails validation, but what I've got doesn't seem to want to work. Can anyone help me…
0
votes
1 answer

Validate uniqueness of child objects in Rails nested form

I have a form for Library, which has several nested objects for Librarian. My question is: How can I validate uniqueness of Librarian's name? (there may be multiple librarians for one library, but there should not be the same librarian appears…
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
0
votes
3 answers

Rails uniqueness validation fails for no reason

I have an scope-based uniqueness validation in place: validates :company_standard, uniqueness: { scope: :company_id } on a Rails Model X. On the attached screenshot I am trying to create a new object of class X for that company via a form (=the…
0
votes
1 answer

Activerecord validates uniqueness does not work with scope and case_sensitive

I'm using Rails 7 and I try to validate the uniqueness of multiple columns using scope regardless of the case, as follow: validates :attr1, uniqueness: { scope: %i[attr2 attr3], case_sensitive: false } The following spec passes it "is not valid…
Jeremie
  • 2,241
  • 2
  • 17
  • 25
0
votes
1 answer

validates_uniqueness custom handling ROR

I would like load the particular record if it fails the validates_uniqueness constraint. I have id (primary key) and title in my model. If the user enters a title which is already present if want to search for the record with the title and load that…
Balaji
  • 31
  • 2
  • 4
0
votes
0 answers

api side validateUniqueness for multiple field in RedwoodJS

According to rw docs we can check uniqueness of fields before starting process like this validateUniqueness('user', { filed: value }, (db)=>{}) But how can we validate multiple field at same time. Note: below wont operate…
mnr
  • 83
  • 7
0
votes
1 answer

validates_uniqueness_of field scoped to a has_one relationship

I have the following models: class Section < ActiveRecord::Base belongs_to :course has_one :term, :through => :course end class Course < ActiveRecord::Base belongs_to :term has_many…
zsalzbank
  • 9,685
  • 1
  • 26
  • 39
0
votes
1 answer

Validate uniqueness of attribute scoped by another attribute presence

If I need to validate uniqueness of cat name scoped by owner so that no owner can have many cats with same name I can use that: validates_uniqueness_of :name, scope: :owner_id But how can I validate the uniqueness scoped by that the cat has owner or…
Ahmed Salah
  • 851
  • 2
  • 10
  • 29
0
votes
1 answer

Adding uniqueness validation for dynamic attributes in rails

We have a multi-tenant application where validation differs for each account. We could easily achieve this for presence validation like the below, module CommonValidator def add_custom_validation required_fields = get_required_fields …
Aarthi
  • 1,451
  • 15
  • 39
0
votes
1 answer

Validate time overlap on a model with polymorphic associations with ruby on rails

I am trying to create a validation in a model (I think it would be better to validate it at a database level actually) so an user can not purchase and item that has already bought in a interval of 2 days (after 2 days it's ok if he/she buys it…
Gotey
  • 449
  • 4
  • 15
  • 41
0
votes
1 answer

Unique value validation: before or after update to persistence layer?

I'm wondering what's the best method for validating a view field value to be unique in an entityset: before or after the update to persistence layer? The involved db field has an unique constraint, and its table is mapped to an EF model. I see two…
fabri
  • 1
0
votes
2 answers

Ruby On Rails Devise: How can I avoid email uniqueness validation during sign up and rewrite to check if it's unique only if has role "X"

I want to interrupt the default registration action in my rails application, changing email validation. All I need is to have email field unique ONLY if that user is already registered AND has role 'X'. I've tried the following but my application…