I am building a rest api with sinatra and datamapper and I have my database file which looks like this:
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!
and I want to make a post method to add an owner to a company
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
but when I tried to run this request I get this error :
curl -d "owner[name]=rrr & owner[id_company]=1" http://localhost:4567/owners
["Company must not be blank"]
Can somebody tell me how to make the association between the company and the owner in the post method?
The problem is that it should not be id_company has to be company_id, changing...
property:id_company,Integer, :required =>true
by
property :company_id,Integer, :required =>true
and doing curl on this way, has to fix this error
curl -d "owner[name]=rrr & owner[company_id]=1" http://localhost:4567/owners