updating a model while in before_update

I've been wondering, suppose I have a model with an attribute that in every instance is dependent on that same attribute in other instances. The best example for this would be an order attribute for items in a list.

The best place to update the rest of the items' order attributes would be in a before_update callback method, were you have both the item's old and new values.

But now whenever you update the other items in the list the callback is going to be called again, and again...

I'm looking for an elegant way of solving this.

I have heard about the :update_without_callbacks method, but i don't want to use a private method, and also i feel like adding extra attributes would be unnecessary.

Got any good ideas? Thanks in advance!


One way would be to use update_all to set the order of all the other items in bulk.

That way you would efficiently limit the number of queries to one and prevent any callbacks from being triggered.


I feel the fact that you have to do this type of update across entries suggests you haven't properly conceptualized your problem. Why not create a List model that has the order attribute, and then create a one-to-many relationship between the List model and the Item model. This way, there's only one place to update the ordering information and no need for complicated and brittle callbacks.

Need Your Help

Grid view with SqlDatasource

c# asp.net .net

I have in my Web a GridView that use SqlDatasource to insert data into the rows of the gridview.

Why is my Google Maps API v2 still working

google-maps google-maps-api-2

I have a google map on my site for showing visitors where events are held, in the back end of the site, the user can enter an address find the location and then drag the marker to the specific loca...