我有2個類叫做User和Gig,還有一個連接表Usergig。Ruby/Sinatra/DataMapper多對多如何創建對象
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
我得到的錯誤:在/演出 NoMethodError /加 未定義的方法'包括「?爲零:NilClass
我已經搜索了大約兩個小時,並閱讀DataMapper文檔。 任何人都知道我做錯了什麼?
我編輯了你的文章,使其可讀。你應該粘貼你的代碼(沒有行號,所以我們可以複製/粘貼它!),然後選擇它並按CMD + K(Mac)或ctrl + K(PC)。 – d11wtq 2011-12-14 20:57:57