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 Hussey
2006-Jan-30 19:28 UTC
[Rails] Re: AR load script only loading every other item?
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 On 1/30/06, Sean Hussey <seanhussey@gmail.com> wrote:> 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 >
Possibly Parallel Threads
- Model.find(:all) where Model.association has more then 0 items?
- exception_notification plugin not sending mails in development on localhost
- O''Reilly Rails Cookbook on Rough Cuts
- MySQL in dev, Postgres in prod - differences in "LIKE" query
- Pagination :conditions not working - MySQL v. PostgreSQL, Rails abstraction v. embedded SQL