0

Is there any shortcut method to set one column data to another in same model?

FYI I do know about for_each method in rails and I can implement that. However what I am trying to ask is if something like User.update_all(last_logged_at: last_sync_position) possible where last_logged_at and last_sync_position are columns of the model user

Also. This is not a migration file I am writing.

I have tried to search for the same but I got an answer in mysql but I am unable to figure out how to convert to rails way of it.

Sagar
  • 477
  • 4
  • 15
  • Possible duplicate of [Rails 3.x How to write update all based on row value?](https://stackoverflow.com/questions/10347618/rails-3-x-how-to-write-update-all-based-on-row-value) – Sagar Nov 03 '17 at 03:39

1 Answers1

0

If i understand it correctly you want to update columnt last_logged_at for every User by last_sync_position(every User have its own value). So u duplicate data on model. Im not sure if this is necessery to solve problem that u have but if you found answer in mysql u could use "scope".

Anyway i found this:

Rails 3.x How to write update all based on row value?

that says this

User.update_all("last_logged_at=last_sync_position")

should work

sonic
  • 1,282
  • 1
  • 9
  • 22
  • Thanks for the solution. This is indeed what I was looking for. As this question is duplicate of the one you shared I will be deleting my question soon. – Sagar Nov 02 '17 at 03:55