所以我在使用 DataMapper 和对象关联时遇到了一些麻烦。(底部提供的代码)。我在保存到未设置某些 id 的情况下遇到错误,我认为这与我如何设置关联/不完全理解关联在 DataMapper 下的工作方式有关。
我正在运行的代码是:
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
我收到的错误是:
~ (0.000064) SELECT "id", "username", "password" FROM "rose_users" WHERE ("username" = 'some-user' AND "password" = 'REDACTED') ORDER 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=)
模型类在这里: http: //www.pastie.org/private/xer5grfaulmnxalne6g5va(链接为简洁)
编辑好的,崩溃来自第 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]
)
那么它是否与从 的继承有关BandwidthEntry/BandwidthDeviceEntry
,因为该类甚至与设备没有关联。
不妨也发布完整的堆栈跟踪: http: //www.pastie.org/private/ospnkeeylul9mhf4fgxhdq
编辑好的,这里基本上是其余的代码http://www.pastie.org/private/pwmihpa6vu3h7lypx64ag
我永远不知道要发布多少,对不起!