0

I have created my first Rails Rake task which imports some data. It uses the URL to identify if the page needs to be updated or inserted. I am however having som really weird issue with some records being inserted multiple times instead, instead of just being updated.

My query looks like this:

existingCompany = Company.find_by_external_link(company.external_link)

I then look at

existingCompany.nil?

to see if the record needs to be created or updated. Some of the companies are not found by active record even though the external link exists. I have tried to print out the url and then look in the database (I use PostgreSQL) and it finds it correctly. The even weirder thing is that it doesn't happen to all records, only a few of them.

Anyone got an idea what might make ActiveRecord believe that a record doesn't exist?

4

1 回答 1

1

您没有提供很多信息,但可以尝试一些事情:

  1. company.external_link 是如何设置的?无论是在调试器中还是在简单的 puts 中,都可以告诉您这是否是您的想法。例如“ http://www.ups.com/”!=“https://www.ups.com/”!=“http://www.ups.com_ _ _
  2. 您可能需要在 company.external_link (.downcase, .strip) 中保持大小写一致或删除空格

要记住的另一件事是 ActiveRecord 创建了一个方法 Company.find_or_create_by_external_link 将在一个步骤中执行此操作

于 2013-02-28T16:44:02.927 回答