kbeegle said over 5 years ago on Database, Model and Scopes :
Good timing on this episode. I'm working on a similar validation where my table has name:string and reviewed:boolean. I want the name to be unique on all reviewed records. On unreviewed records, the name can be blank or even a duplicate. In the app, I'm planning to use the validation: ``` validates :name, uniqueness: { scope: :reviewed }, if: :reviewed? private def reviewed? self.reviewed == true end ``` How would you go about matching a similar uniqueness constraint in the database? The following constraint would limit me to a single unnamed/unreviewed record. ``` add_index :listings, [:name, :reviewed], unique: true ``` Any thoughts?