[Rails] Validations and has_many :through (Join Models)

Jean-François jf.web3 at gmail.com
Tue Apr 11 20:54:16 GMT 2006


Hello David,

[...]
> After all of that, there now exists 2 records in the subscriptions
> table, both of which have the same reader_id and magazine_id.
>  How do you perform the validation to make sure that each record
> in the subscriptions table has a unique reader_id, magazine_id
> pair?

Try this :

class Subscription < ActiveRecord::Base
  belongs_to :reader
  belongs_to :magazine

  validates_uniqueness_of :reader_id, :scope => :magazine_id
end

Good night,

    -- Jean-François.

--
À la renverse.


More information about the Rails mailing list