Forum: Ruby on Rails AR load script only loading every other item?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sean H. (Guest)
on 2006-01-30 20:53
(Received via mailing list)
Wow, this one is weird.  I've got a file with data I want to import,
one item per line.  When I run this code, only the first, third,
fifth, etc items get saved:

require '../app/models/entitlement'

#### Add entitlements

f = File.open("../tmp/entitlements.csv")
f.each do |line|
  id, code, description = line.chomp.split('|')
  entitlement = Entitlement.new
  entitlement.code = code
  entitlement.description = description
  entitlement.save
end
f.close

Where am I going wrong?  It's just not saving the second, fourth, etc
items.

If I test the saving, that confirms that it's not saving, but I don't
know why:

...
  if entitlement.save
    p "Saved #{code}"
  end
...

"Saved wireless"
"Saved vpn"
"Saved trac"
"Saved svn"

The file:

1|wireless|Wireless Network
2|wirelessStudent|Student Wireless Network
3|vpn|VPN
4|email|Email Account
5|trac|Trac
6|calendar|Calendar Account
7|svn|Source Code Repository
8|directory|Directory Listing

Any ideas?  I know I've run this import before and it's worked.

Thanks!

Sean
Sean H. (Guest)
on 2006-01-30 22:46
(Received via mailing list)
Answered it myself (I think).  In the model, don't put:

validates_uniqueness_of :id

I have no idea why that one just hit me now, but at least, after
removing that, everything works.

Sean
This topic is locked and can not be replied to.