mercredi 27 mai 2015

A Very Specific ActiveRecord Validation

I'm will try to explain my dilemma as best I can.

I have a 2 models Users and Devices

Devices has a type column. For example it can be a Tablet or a Smart Phone.

I then have another model Ownership

This model belongs to both Users and Devices so it will have entries in the table matching users to devices representing that that user owns that device.

I need to setup some validations on the Ownership model the primary ones begin.

  • No device can have more than one ownership entry (simple as validating that device_id column is unique)
  • A user can only have ownership of one device for each type of device available.

So how would I make a validation for this last part? I can't set the client_id table to unique because they will have multiple entries for different types of devices. But I want them to only have one row per type of device.

I hope this makes sense. Please let me know if you need more details. I would appreciate any insight!

Thanks!

Aucun commentaire:

Enregistrer un commentaire