Ruby on Rails validation by pair of properties

2019-08-26 03:22发布

问题:

I need to do validation on my model on Ruby on Rails.

I have model with 2 fields:

user_id some_item_id

And I have many rows in DB with the same user_id, but with different some_item_id. And I need avoid duplication of this some_item_id towards to specific user_id.

In Rails exist some 'magic' validation in one line for this?

回答1:

validates : user_id, :presence => true, :uniqueness => { :scope => : some_item_id }