0

https://api.rubyonrails.org/v5.0.1/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-reset_counters

I have a field called sequence in my model. I want to reset all sequences of particular record set and update all sequences one by one 1. But while using reset_counter I'm getting has no association called 'sequence'. Here is my code block

ids = [1,2,4,6,7]
Group.reset_counters(ids, "sequence")
Group.increment_counter(sequence, ids)

Is it any way to update given ids sequence by 1 (each id sequence should be updated by 1 to the previous id)?

PS: update_attribute is taking time for updating individual records. so i want to update records in bulk.

Vasilisa
  • 4,604
  • 3
  • 20
  • 25
Ankita Agrawal
  • 422
  • 5
  • 20
  • If you're trying to update sequence column of a model Group then [`update_all`](https://apidock.com/rails/ActiveRecord/Base/update_all/class) - `Group.where(id: ids).update_all(sequence: 1)` – Surya Nov 28 '18 at 12:36
  • @Surya this will update all sequence to 1. I want to update sequence increment sequence by 1 to the previous records – Ankita Agrawal Nov 28 '18 at 12:39
  • @AnkitaAgrawal : Provide Group model code and the association on which sequence is updated when cached. – Surya Nov 28 '18 at 12:44
  • @Surya It dont have a sequence association. It has a Sequence as a column_name – Ankita Agrawal Nov 28 '18 at 12:53
  • Did you try [`update_counters`](https://api.rubyonrails.org/v5.0.1/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-update_counters)? – 31piy Nov 28 '18 at 13:00
  • @31piy update_counters will update sequence by 1 with existing id sequence. I want to reset that and update all records one by one – Ankita Agrawal Nov 28 '18 at 13:04

0 Answers0