Problem with validates_presence_of in linking table

Hi,

I have the following models:

class Contact < ActiveRecord::Base
has_many :contact_lists, :dependent => true
has_many :lists, :through => :contact_lists
end

class List < ActiveRecord::Base
has_many :contact_lists, :dependent => true
has_many :contacts, :through => :contact_lists
end

class Contact_List < ActiveRecord::Base
belongs_to :lists
belongs_to :contacts
end

I included a validates_presence_of :list_id, :contact_id in
Contact_List, but I am getting a nil.collection error when I submit
without any values. Any ideas?

Also, can I use validates_uniqueness_of :list_id, :contact_id to prevent
duplicate records from being added to Contact_List?

Thanks,

David

Try validates_presence_of :list, :contact

validates_uniqueness should work.

And it’s belongs_to :list; belongs_to :contact (singular)

Vish

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs