我有两个类叫做User和Gig,还有一个连接表Usergig。
class Usergig
include DataMapper::Resource
property :id, Serial
belongs_to :user
belongs_to :gig
end
class Gig
include DataMapper::Resource
property :id, Serial
property :gigname, String
property :gigtext, Text
has n, :usergigs
has n, :users, :through => :usergigs
end
class User
include DataMapper::Resource
property :id, Serial
property :username, String
property :realname, String
has n, :usergigs
has n, :gigs, :through => :usergigs
end
当我试图运行:
post '/gig/add' do
user = User.get(1)
gig = user.gigs.create(:gigname => params[:gig_gigname], :gigtext => params[:gig_gigtext])
end
我得到错误:在/gig/add处出现错误未定义的方法' include?' for nil:NilClass
我已经在谷歌上搜索了大约两个小时,并阅读了DataMapper文档。有人知道我哪里做错了吗?
在Usergig中尝试以下操作:
belongs_to :user, :key => true
belongs_to :gig, :key => true
您忘记调用DataMapper.finalize…这是加载所有模型后需要调用的函数。在Sinatra中,你必须手动调用它