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

Why doesn't SparseIntArray implement Map<Integer, Integer>?

java android api collections

The API documentation for Android's SparseIntArray opens with:

Read a file in C++ from a specific Line and then store it in Vectors/Array

c++ file c++11 graph stdvector

I am new to C++ programming and I did my homework for reading a file. I am learning C++ from this cpp-tutorial: Basic-file-io site.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.