Skip to content

Check if a uniqueness validation has been applied in a relation #1494

@cesarjr

Description

@cesarjr

Sometimes it's possible that, by mistake, a uniqueness validation is applied in the relation instead of its foreign key:

class User < ApplicationRecord
  belongs_to :seller
  
  # wrong:
  validates :seller, uniqueness: true

  # correct:
  # validates :seller_id, uniqueness: true
end

In this situation, it's possible that an N+1 query issue could happen in saving multiple User records at once. This problem could be avoided applying the validation in seller_id instead of seller.

If this ideia is interesting, I can ask for my team to implement it. But I don't want to waste time if it is not interesting for the project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions