DataMapper associations/validation causing save failure
So I'm having some trouble with DataMapper and object associations. (Code provided at bottom). I'm getting errors upon saving to the extent that some id's are not set, which I think has something to do with how I setup my associations/not fully understanding how associations work under DataMapper.
The code I'm running is:
Rose.setup_datamapper
Rose::User.first_or_create(:username => 'some-user', :password => 'REDACTED')
Rose::User.all.each do |user|
user.scrape_and_update
user.errors.each { |e| puts e } unless user.save
user.bandwidth_entries.each { |e| puts e }
end
And the errors I am recieving are:
~ (0.000064) SELECT "id", "username", "password" FROM "rose_users" WHERE ("username" = 'some-user' AND "password" = 'REDACTED') O开发者_C百科RDER BY "id" LIMIT 1
~ (0.000042) SELECT "id", "username", "password" FROM "rose_users" ORDER BY "id"
~ rose_bandwidth_entries.device_network_address may not be NULL (code: 19, sql state: , query: INSERT INTO "rose_bandwidth_entries" ("policy_mbytes_received", "policy_mbytes_sent", "actual_mbytes_received", "actual_mbytes_sent", "timestamp", "bandwidth_class", "user_id") VALUES (583.34, 39.58, 590.27, 44.26, '2011-09-20T13:39:31-04:00', 0.0, 1), uri: sqlite3:/Users/axiixc/Dropbox/Ruby/stats.sqlite?port=&adapter=sqlite3&fragment=&path=/Users/axiixc/Dropbox/Ruby/stats.sqlite&scheme=sqlite3&host=&user=&password=&query=)
Model classes are here: http://www.pastie.org/private/xer5grfaulmnxalne6g5va (link for brevity)
EDIT Okay the crash is coming from create on line 26:
# /Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:114:in `execute_non_query': rose_bandwidth_entries.device_network_address may not be NULL (DataObjects::IntegrityError)
main_entry = BandwidthMainEntry.create(
:user => self,
:timestamp => Time.new,
:policy_mbytes_received => scrape_dict[:main][:policy_mbytes_received],
:policy_mbytes_sent => scrape_dict[:main][:policy_mbytes_sent],
:actual_mbytes_received => scrape_dict[:main][:actual_mbytes_received],
:actual_mbytes_sent => scrape_dict[:main][:actual_mbytes_sent],
:bandwidth_class => scrape_dict[:main][:bandwidth_class]
)
So would it have something to do with the inheritance from BandwidthEntry/BandwidthDeviceEntry
, because that class doesn't even have an association with a device.
Might as well post the full stack trace too: http://www.pastie.org/private/ospnkeeylul9mhf4fgxhdq
EDIT Okay, here is basically the rest of the code http://www.pastie.org/private/pwmihpa6vu3h7lypx64ag
I can never tell how much to post, sorry!
Okay, so I derped on inheritance, and should have read more of the documentation. What was happening was I missed the property :type, Discriminator
needed when using subclasses. So even though my one subclass didn't need a device associated with it, the other did, so that's what was triggering the error.
My working model looks like this: http://www.pastie.org/2564668
At a guess, the value device_dict[:network_address]
isn't getting set properly - that error indicates you're inserting a NULL
device address, which comes from a nil
value in your create
call. Try checking the values in device_dict
when you go to create device entries.
精彩评论