Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get RETURNING after updating a record in rails

how to make this query in activerecord:

UPDATE table_name SET column = 'value' WHERE id = 123 RETURNING other_column

I tried update_all but it returns number of changed records, update_atributes which returns true/false, and update which returns the whole object.

In rails it should look like this (pseudo-code):

other_column = TableName.where(:id => 123).update_attributes(:column => 'value').returning(:other_column)
puts other_column
like image 707
Nicolae Rotaru Avatar asked Nov 01 '22 03:11

Nicolae Rotaru


1 Answers

result_sets = Model.connection.execute("UPDATE table_name SET column = 'value' WHERE id = 123 RETURNING other_column")
result_sets.each do |result|
  p result[:other_column]
end

If you just want to update a single record with id 123, why can't you do this?

   TableName.update(123, :column => 'value').other_column
like image 192
usha Avatar answered Nov 15 '22 03:11

usha