I have built my Rails model schema to match the iphone contacts, which includes multi-value email associations, etc. I have a controller action that imports an entire contacts array (likely 1,000+ objects, each potentially containing multiple email objects). I need this to run fairly efficiently, so I was looking at activerecord-import for batch importing. However, I need to validate the uniqueness of the email within the scope of each contact so that I don't keep adding duplicates every time the batch is imported. Should I build my own version of update_attributes by hand, or is there an existing solution that you might recommend for validating/updating lots of records like this?
Contact Model
class Contact > ActiveRecord::Base
has_many :addresses
has_many :emails
has_many :websites
accepts_nested_attributes_for :addresses, :emails, :websites
attr_accessible :prefix, :first_name, :middle_name, :last_name, :suffix,
:nickname, :organization, :job_title, :department, :birthday,
:addresses_attributes, :emails_attributes, :websites_attributes
end
Email Model
class Email > ActiveRecord::Base
belongs_to :contact
# validates_uniqueness_of :account, :scope => :contact_id # prevents duplicate, but also skips sibling values
# validates :contact_id, :presence => true, :on => :create # causes 422 error
validates :account, :presence => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
attr_accessible :contact_id, :email_id, :account, :label
end