Diego Dias
Diego Dias

Reputation: 914

sinatra + mongoid

I'm creating an app that use sinatra + mongoid. I have two models, contact has many phones. To test my sinatra controller I post my data with this command `

curl -X POST -d "contact[name]=nome&contact[email][email protected]&contact[phone][0][number]=88888888&contact[phone][0][type]=1&contact[phone][2][number]=77777777&contact[phone][3][type]=1"

but when I did one query in mongodb I see that not save as expected. I need that phone class will be save with array, but now phone is a hash where the key is "0", "1", N like my post data. How do I to resolve this problem? I want data to be saved so:

{
"_id":"4f889875b336e722a0000003",
"email":"[email protected]",
"github":"diegodfsd",
"name":"diego2",
"phone":{
"0":{
"number":"89311768",
"type":"cellphone"
},
"1":{
"number":"55555555",
"type":"home"
}
},
"twitter":"diegodfsd"
}

gist

Upvotes: 0

Views: 494

Answers (1)

shingara
shingara

Reputation: 46914

You need use phones_attributes params instead of phone

curl -X POST -d "contact[name]=nome&contact[email][email protected]&contact[phones_attributes][0][number]=88888888&contact[phones_attributes][0][type]=1&contact[phones_attributes][2][number]=77777777&contact[phones_attributes][3][type]=1"

Upvotes: 1

Related Questions