Suppose I have:
class Author
has_many :books
class Book
belongs_to :author
validates :name, :presence => true, :uniqueness => true
I want to change this so that the name of the book is only unique within the scope of the author, i.e. no author has two books with the same name, but two authors could have a book with the same name. Is this possible?
It's very possible and quite easy:
validates :name, :presence => true, :uniqueness => {scope: :author}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With