Hi,
Here is my Sample Code... I am calling the genericCURDoperation action through the view and sending the xml containing the record to be inserted/updated along with its key value(the primary key field and it's value in the xml)
The controller action is
def genericCURDoperation
companyid = session[:companyid]
obj = MGenericCurdOperation.new(params[:objects],companyid)
result = obj.genericAction
puts result
end
In my MGenericCurdOperation model
class MGenericCurdOperation
attr_accessor :companyid,:objects
def intialize(xml,companyid)
@objects = xml
@companyid = companyid
end
def genericAction
hash = @objects.to_hash
operation = hash['operation']
if operation == "insert" then
status = saveToDB(hash['record'],companyid)
elsif operation == "update" then
status = UpdateToDB(hash['record'],companyid)
.................
end
return status
end
def saveToDB(record,companyid)
............
end
def UpdateToDB(record,companyid)
hash = record.to_hash
key = hash['key']
if key==nil then
puts " the key not coming thourgh xml"
return nil
end
value = hash[key.to_s]
model = hash['model']
record = model.find(:all,:conditions=>["#{key}=?",value]) #retriving the record from db
updateRecord = model.new(hash['record']) # record with updated fileds ,consits every filed in the record except the key value,attribute
updateRecord.TimeUpdate = DateTime.now # Here whatever the other requirements and conversions added to the updateRecord and also checking the required fields to be non empty etc;
recordHash = Hash.from_xml(updateRecord.to_xml)
root = recordHash.keys
rootElement = root(0)
if record.update_attributes(recordHash[rootElement.to_s]) then
puts record.to_xml
puts "saved successfully"
redirect_to 'index'
end
end
end
this is my code with some more operations also.When I debug the control going to the update_attributes but it is not updating in database.even the record.to_xml showing the contents as updated but in can’t be viewed in database and also on index page. Can anyone please help me due to this my application not working and can’t be moved to the production.