3

How to write this using the Sequel gem? :

update table set col = (select col from table where id = :x) where id = :y

I have the record with id=:y but I want to avoid retrieving the record with id(:x) and then doing the update. Just one step! Do I have to use raw sql (DB.run())?

Aleksey
  • 2,289
  • 17
  • 27
MegaTux
  • 1,591
  • 21
  • 26

1 Answers1

3
DB[:table].where(:id=>y).update(:col=>DB[:table].where(:id=>x).select(:col))
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jeremy Evans
  • 11,959
  • 27
  • 26
  • That's really nice! I'll try later. This will perform only one sql command, right? – MegaTux Jul 12 '16 at 21:04
  • BTW, I had to put this statement inside a transaction block to make it work ( `DB.transaction do ... end` ) – MegaTux Jul 25 '16 at 20:03
  • I had to rollback to my previous code. I had some trouble persisting the changes, even with a transaction block. Strangely, some runs didn't save changes. – MegaTux Aug 12 '16 at 15:43