我有两个映射为datamapper的表:
class User
include DataMapper::Resource
property :id, Serial, :field => 'ID'
property :user_first_name, String, :field => "USER_FIRST_NAME"
property :user_last_name, String, :field => "USER_LAST_NAME"
has n, :login_sessions, :child_key => [ :user_id ], :parent_key => [ :id ]
end
和
class LoginSession
include DataMapper::Resource
property :id, Serial, :field => 'ID'
property :user_id, Integer, :field => "USER_ID"
property :login_time, DateTime, :field => "LOGIN_TIME"
belongs_to :user, :parent_key => [ :user_id ], :child_key => [ :id ]
end
给定一个LoginSession对象,我想得到相应的用户:
session = LoginSession.first(:id => 1)
if session
# code reaches here, meaning that session is not nil.
user = session.user
end
当我运行此代码时,在user = session.user,我收到以下错误:
NoMethodError - undefined method `get!' for nil:NilClass:
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm- core/property_set.rb:88:in `block in get!'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `block in each'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `block in each'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `each'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/property_set.rb:88:in `map'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/property_set.rb:88:in `get!'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/associations/many_to_one.rb:289:in `source_key_different?'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/associations/many_to_one.rb:179:in `lazy_load'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/persisted.rb:23:in `lazy_load'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/persisted.rb:8:in `get'
/Users/Tom/.rvm/gems/ruby-1.9.3-p362/gems/dm-core-1.2.0/lib/dm-core/model/relationship.rb:340:in `user'
/Users/Tom/Projects/api_server/app.rb:197:in `block in <class:MyApp>'
...
我无法弄清楚我做错了什么。我假设LoginSession对象找不到相应的用户方法或什么?请帮忙!