代码之家  ›  专栏  ›  技术社区  ›  DGM

(NULL)中的NULL不正确匹配

  •  0
  • DGM  · 技术社区  · 14 年前

    使用Rails 3活动关系,我有一个范围:

      scope :duplicate_contact, lambda {|contact| where(
                      :person_id          => contact.person_id,
                      :salutation         => contact.salutation,
                      :first_name         => contact.first_name,
                      :last_name          => contact.last_name,
                      :suffix             => contact.suffix,
                      :birthday           => contact.birthday,
                      :address            => contact.address,
                      :city               => contact.city,
                      :state              => contact.state,
                      :zip                => contact.zip,
                      :phone_1            => [contact.phone_1,contact.phone_2,contact.phone_3],
                      :phone_1_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                      :phone_2            => [contact.phone_1,contact.phone_2,contact.phone_3],
                      :phone_2_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                      :phone_3            => [contact.phone_1,contact.phone_2,contact.phone_3],
                      :phone_3_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                      :email              => [contact.email,contact.alternate_email],
                      :alternate_email    => [contact.email,contact.alternate_email]
                )
              }
    

    当:email为NULL时出现问题。它返回0行,而实际上它至少应该返回1行,即 duplicate_contact(contact).size == 0 当它应该是假的时候,它才是真的。

    我怎样才能得到正确的结果呢?

    1 回复  |  直到 14 年前
        1
  •  0
  •   DGM    14 年前

    我发现了一个可能的解决方案:

     scope :duplicate_contact, lambda {|contact| 
    
        q = where(
          :person_id          => contact.person_id,
          :salutation         => contact.salutation,
          :first_name         => contact.first_name,
          :last_name          => contact.last_name,
          :suffix             => contact.suffix,
          :birthday           => contact.birthday,
          :address            => contact.address,
          :city               => contact.city,
          :state              => contact.state,
          :zip                => contact.zip
        )
        [contact.phone_1,contact.phone_2,contact.phone_3].compact.each{|p| q=q.has_phone(p)}
        [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type].compact.each{|p| q=q.has_phone_type(p)}
        [contact.email,contact.alternate_email].compact.each{|p| q=q.has_email(p)}
        q
      }
      scope :has_phone, lambda {|phone|
        where("'#{phone}' IN (phone_1,phone_2,phone_3)")
      }
      scope :has_phone_type, lambda {|phone|
        where("'#{phone}' IN (phone_1_type,phone_2_type,phone_3_type)")
      }
      scope :has_email, lambda {|email|
        where("'#{email}' IN (email,alternate_email)")
      }
    
    推荐文章