Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8599345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:22:41+00:00 2026-06-12T01:22:41+00:00

Let me get started by saying I am a Rails newbie so I’m going

  • 0

Let me get started by saying I am a Rails newbie so I’m going to try and explain this the best that I can. As a reference, please see this thread which describes exactly what is happening to me, except the fix doesn’t seem to be working.

Problems with limit method in Rails 3.2 and hash ordering

I have a database with events that I am trying to sort by the primary key “id”. I tried following the Railscast on sortable table columns here:

http://railscasts.com/episodes/228-sortable-table-columns

The sorting works properly but the sort happens on the entire column instead of just the “last 50”.

Controller Code:

def index
  @events = Event.order(sort_column + " " + sort_direction).page(params[:page]).last(50)
  @events = @events.sort_by(&:id)
  @events = Kaminari.paginate_array(@events).page(params[:page]).per(10)
end

def sort_column
  Event.column_names.include?(params[:sort]) ? params[:sort] : "event_id"
end

def sort_direction
  %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end

Application_helper:

    def sortable(column, title = nil)
  title ||= column.titleize
  direction = column == sort_column && sort_direction == "desc" ? "asc" : "desc"
  link_to title, :sort => sort_column, :direction => direction
end

Finally, in the index view, I call it like this:

<th><%= sortable  "event_id" %></th>

I’m sorry if this question has already been answered, any help on this subject would be greatly appreciated.

EDIT

Thanks for the quick reply. I understand what you did there, but now I get an exception for some reason in my index view.

 undefined method `id' for #<Array:0x00000004170c10>

Extracted source (around line #25):

22:   <tbody>
23:     <% @events.each do |event| %>
24:       <tr>
25:         <td><%= link_to event.id, event_path(event) %></td>
26:         <td><%= event.event_date_time_local %></td>
27:         <td><%= EventType.find(id =    event.event_type_id).event_type %></td>
28:         <td>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:22:43+00:00Added an answer on June 12, 2026 at 1:22 am

    You said “instead of the last 50” but that is all you are returning. The last 50 found items after sorting based on sort_column and direction.

    @events = Event.order(sort_column + " " + sort_direction).page(params[:page]).last(50)
    #Sorted the last 50 by sort_column and direction
    
    @events = @events.sort_by(&:id)
    #sorted again by id and lost the first sort
    

    To get all events and then sort the last fifty you can do

    @events = Event.order(sort_column + " " + sort_direction).page(params[:page])
    last_fifty = @events.pop(50) # remove last 50 and return it
    
    @events << last_fifty.sort_by(&:id) # insert last fifty back sorted by id
    

    Edit:

    Sorry, instead of

    @events << last_fifty.sort_by(&:id)
    

    try

    @events.concat last_fifty.sort_by(&:id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's get this out of the way first: I know that SessionFactory is immutable
Are there any Python modules that will let me get access to NFS quota
Can someone please let me know how to get values from several input fields?
Let me start out by saying that I am new to Java/JSP web development,
This is my first post, so let me start by saying HELLO! I am
I really need to get started with source control because I'm going absolutely crazy
I am trying to get started with OOAD, this problem came to my mind
Let me start by saying that I don't know what I don't know at
Ok, let's get straight to the point. I got 2 questions 1. Why self.StyleSetSpec(stc.STC_STYLE_DEFAULT,
My UIWebView loads a contenteditable html content. I want to let the UIWebView get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.