我建立我的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