user1098667
user1098667

Reputation: 31

Ruby/Sinatra/DataMapper Many-to-Many how creating an object

I have 2 classes called User and Gig and also a joining table 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

And when i try to run:

  post '/gig/add' do
    user = User.get(1)
    gig = user.gigs.create(:gigname => params[:gig_gigname], :gigtext => params[:gig_gigtext])
  end

I get the error: NoMethodError at /gig/add undefined method `include?' for nil:NilClass

I've googled for about two hours now and read the DataMapper documentation. Anyone know what i'm doing wrong?

Upvotes: 3

Views: 760

Answers (2)

solnic
solnic

Reputation: 5743

You forgot to call DataMapper.finalize...this is what you need to call after all your models are loaded. Rails does this for you, in Sinatra you must call it manually.

Upvotes: 1

Zefiryn
Zefiryn

Reputation: 2265

In Usergig try the following:

belongs_to :user, :key => true
belongs_to :gig, :key => true

Upvotes: 1

Related Questions