0

I'm working with a js framework called spine. The framework can do all crud operations through REST api(like backbone). When I get a create request to my controller, I get a model data that has an id field from client side, I know that I'm in a create request so I should create a new record but mongo think this record already exists(because of the id field) and invokes update instead of insert. Is there a way to solve this issue? I want client side to create this temporal id's but when calls server side create this id should be ignored.

some code:

#in my_controller
def create
  @model= Model.new(params[:modelData]) #model data is name=>"x", _id=>"c-0"
  @model.save

when save is called mongo do update, so next create will override this record.

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141

2 Answers2

0

I'm not quite sure I understand your question, but I think you want this:

@model = Model.find_or_initialize_by_id(params[:modelData][:_id])
@model.update_attributes(params[:modelData])
Brian Hempel
  • 8,844
  • 2
  • 24
  • 19
0

I had no choice but deleting the id field from the model I got from the client.

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141