I have an opportunity to upgrade resources from Rails3 to Rails5, and I haven't understood the method specifications.

What you do n’t understand

About sort method in the environment where will_pagination is introduced

In Rails3

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

The sort was using the sort! Method on the instance obtained from the model like this

An error occurs when moving to Rails5.
I can go ahead by stopping the disruption as follows

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

This is the will_pagination
undefind method 'total_pages' for Array
Will result in an error.

In the total_pages method of will_pagination, we expect the value of (@resources.) total_entries included in the willpagination :: collection class, so it is understood that an error will occur if passed in the Array class. I don't know why Rails3 doesn't become an Array even if it goes through the sort method. The return value of the sort method is an Array class, right?
* When logging in Rails3 environment, willpagination :: collection was maintained even after passing the sort method.

I think it's a specification change, but I can't find the documentation and I'm not confident in the implementation.

I want to tell
  • Why sort! (destructive method) cannot be used in Rails5 * Rails3 can use it.
  • Why is Rails3 not returning the Array class even after passing the sort method? (Does destructive methods have to change the instance class?)
  • I saw an article that will_pagination works even if it is passed as an array, but if I pass Array, an error will occur in total_pages. Is there an improvement approach to this?
  • Answer # 1

    @resources = BookLibrary.order (: code) .paginate (: page =>params [: page])
    Is that okay?

    I have done this.

    @Models = @Relation # .to_a
          # From here Will-Pagenat Damashi
          # Relation # count with group (customer_id) does not return the total number customer_id
          # Cannot visit Customer :: ActiveRecord_Relation or
          # ActionView :: Template :: Error (undefined method `total_pages'
          # cause. So
          # for will_paginate (@Models) called in view
          # Define method total_pages and current_page in @Models
          # @models does the same search without using pagenate.
          eval "def @ Models.count;super.size;end
                def @ Models.total_pages;(self.count/# {@ Pagenation.to_f}). ceil;end
                def @ Models.current_page;# {@ page};end "
          @models = @ Models.offset ((@ page-1) * @ Pagenation) .limit (@Pagenation)


    this time
    @resources = BookLibrary.paginate (: page =>params [: page])
    total_pages = @ resources.total_pages
    @resources = @ resources.sort {| a, b | a.code.to_i<=>b.code.to_i}
    eval "def @ resources.current_page;# {@ page};end
    def @ resources.total_pages;# {total_pages};end "

    How about it? . .

    Additional notes
    Don't go that far
    Pass @resources to WillPagenate
    Use@resources_sorted = @ resources.sort {| a, b | a.code.to_i<=>b.code.to_i}to draw data

  • Answer # 2

    array How about replacing only the contents?

    @ resources = BookLibrary.paginate (: page =>params [: page]). to_a
    @ resources.replace (@ resources.sort {| a, b | a.code.to_i<=>b.code.to_i})