datamapper用にマップされた2つのテーブルがあります。
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オブジェクトが対応するユーザーメソッドなどを見つけることができないと思いますか?助けてください!