Update method in Rails -


i need update table(mysql db) json received different table controller. have "lists" controller(for list table in db) , "table" (for tables table in db)controller. json insert new row in lists table. json received, need pick table number , update tables table too. below json recieved

started post "/lists.json" 192.168.1.2 @ 2013-08-26 16:55:51 +0530   processing listscontroller#create json   parameters: {"list"=>[{"amount"=>"10.50", "orderno"=>"0220130826163623", "quan tity"=>"1", "itemname"=>"patatas ali oli", "tableno"=>"02", "ordstatus"=>"ordere d"}, {"amount"=>"10.50", "orderno"=>"0220130826163623", "quantity"=>"1", "itemna me"=>"garlic bread", "tableno"=>"02", "ordstatus"=>"ordered"}, {"amount"=>"12.50 ", "orderno"=>"0220130826163623", "quantity"=>"1", "itemname"=>"entrecote la p lancha", "tableno"=>"02", "ordstatus"=>"ordered"}, {"amount"=>"10.50", "orderno" =>"0220130826163623", "quantity"=>"1", "itemname"=>"pollo al horno", "tableno"=> "02", "ordstatus"=>"ordered"}]} 

from above json need pick "tableno"=>"02" , update tables row tableno=02. below code have written in lists controller :

def create     lists = params[:list].collect{|list_attributes| list.new(list_attributes)}     table = table.find(:tablabel => list.tableno,:tabstatus => 'reserved');      valid,not_valid = lists.partition{|list| list.valid?}      if not_valid.blank?       lists.map(&:save)       @lists = lists       format.html { redirect_to @list, notice: 'list created.' }       format.json { render json: @list, status: :created, location: @list }     else       format.html { render action: "new" }       format.json { render json: @list.errors, status: :unprocessable_entity }     end   end 

problem lists table getting updated extracting , updating tables part not working. beginner in rails not sure missing. please advise. thanks.

look collect objects in lists variables in spite of list.tableno use lists.first.tableno or lists[i].tableno here can 1,2....any number

so

rails >= 3.2

for find

table=table.where("tablabel = ? , tabstatus = ?",lists.first.tableno, 'reserved' )

for update

table.first.update_attributes(tablabel: lists.first.tableno, tabstatus: 'reserved' )


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -