Sinatra 和 post datamapper 协会

Sinatra and post datamapper association

我正在使用 sinatra 和 datamapper 构建一个 rest api,我的数据库文件如下所示:

require 'data_mapper'

DataMapper.setup(:default,'sqlite::memory:')


class Company
 include DataMapper::Resource

 property :id, Serial
 property:name, String, :required => true
 property:adress,String,:required => true
 property:city,String, :required => true
 property:country,String,:required => true
 property:email,String
 property:phoneNumber,Numeric
 has n, :owners, :constraint => :destroy

end

class Owner
 include DataMapper::Resource

 property :id,Serial
 property:name,String, :required => true
 property:id_company,Integer, :required =>true

 belongs_to:company
end

DataMapper.finalize
DataMapper.auto_migrate!

我想制作一个 post 方法来将所有者添加到公司

post '/owners'do
  content_type :json
  owner = Owner.new params[:owner]
  if owner.save
    status 201
  else
    status 500
    json owner.errors.full_messages
  end
end

但是当我尝试 运行 这个请求时,我得到了这个错误:

curl -d "owner[name]=rrr & owner[id_company]=1" http://localhost:4567/owners
["Company must not be blank"]

谁能告诉我如何在 post 方法中建立公司和所有者之间的关联?

问题是不应该id_company必须是company_id,正在改变...

 property:id_company,Integer, :required =>true

来自

property :company_id,Integer, :required =>true

并以这种方式卷曲,必须修复此错误

curl -d "owner[name]=rrr & owner[company_id]=1" http://localhost:4567/owners