I have a cards table with an attribute called 'position', which has a default value of 0.
I need to select a group of cards, then assign the position of each card with an incremented value.
So, lets say that I select a group of cards using
cards = Card.where(id: [3,4,7,8]).
Can a single Activerecord or SQL statement assign an incremented value, that would give a result like this?
cards[0].position
=> 1
cards[1].position
=> 2
...
ActiveRecord, no.SQL, of course, always.The issue here with
ActiveRecordis that you're trying to do two things simultaneously.n, wheren += 1for each record.The problem is that when we're querying, we leave the Ruby execution and entering SQL.
The updated value will be
2for all the records.It's easy to update all the records with the same value, however we aren't updating all records with the same value here.
So you can't use ActiveRecord like this.
If you want to use Ruby to keep the counter, you must do something like:
Otherwise, you will need to do some SQL work.