Home>

Since I thought "Is it?"

About the return value of the sort method, but when I implemented the following in Rails3

@model = Model.paginate (: page =>params [: page])
@ model.sort! {| a, b | a.code.to_i<=>b.code.to_i}


@model object passed through sort! is an instance object of paginate

The following implementation in Rails5

@model = Model.paginate (: page =>params [: page])
# Does an error occur when I use the destructive method of sort on an object of Relation class?
@model = @ model.sort {| a, b | a.code.to_i<=>b.code.to_i}


The object of @model after sorting is Array.

There are the following unclear points from the above point of view. Does anyone know this?

  • Is the Array after the sort due to the specification change after Rails3?
  • If not, why does Rails3 keep the class instance of the original object?
  • Why is the destructive sort available in Rails3 no longer available in Rails5?

Since it is not found even if you search on the net, thank you.

  • Answer # 1

    How to maintain relations is very different between Rails 3 and 4 and later.

    Rails 3gets alland stores it as an array, whereas Rails 4 and later have onlywhat SQL to throw, and data Is an object that is different from an array that is acquired when it is actually needed.

      

    Is the Array after the sort due to a specification change after Rails3?

    No, I think this will be the same in Rails 3. The actual call is aRuby standardmethod calledEnumerable # sort(Rurima), which returns aArray.

      If not

    Why Rails3 can hold the class instance of the original object
      Why is the destructive sort available in Rails3 no longer available in Rails5?

    In Rails 3, the original relation is an array derivation