Implications of has_many :segments, :order => 'position ASC'?
In a Rails 2.3.5 project I found this line of code in a model:
class Foo
has_many :segments, :class_name => 'FooSegment', :order => 'position ASC'
end
The underlying table of FooSegment has an integer attribute position. And as one might guess, the many FooSegments belonging to a Foo have positions 1, 2, 3...
So can I assume that ActiveR开发者_JAVA百科ecord always updates this position attribute behind the scenes? In particular: having a Foo with id=11, having 5 FooSegments. Is it save to assume that they always have positions 1, 2, 3, 4, 5 and keep this order if a FooSegment is updated?
Thanks, Philip
No, that's not functionality that AR has built-in. However, the project may be using the acts_as_list gem or something similar to provide this functionality. By default it uses a column named position
and then keeps it up-to-date when other records change.
精彩评论