我建立我的Rails模型模式以匹配iphone contact s,這包括多值電子郵件協會等我有出口的整個觸頭陣列(可能1,000+對象,每個潛在的控制器動作包含多個電子郵件對象)。我需要這個運行相當有效,所以我在activerecord-import批量導入。但是,我需要在每個聯繫人的範圍內驗證電子郵件的唯一性,以便每次導入批次時都不會添加重複項。我應該建立自己的update_attributes
版本用手,或者是有,你可能會推薦用於驗證/更新大量的這樣的記錄現有的解決方案嗎?使用Rails同步處理iphone觸點
接觸模型
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
電子郵件型號
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
非常有幫助,謝謝。現在,我在服務器上推出了自己的方法。我最終會嘗試在客戶端進行驗證,並嘗試當時的activerecord-import。 – 2012-01-03 02:14:41