AR load script only loading every other item?


#1

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


#2

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