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 537333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:54:57+00:00 2026-05-13T09:54:57+00:00

I have a complex table pulled from a multi-ActiveRecord object array. This listing is

  • 0

I have a complex table pulled from a multi-ActiveRecord object array. This listing is a combined display of all of a particular user’s “favorite” items (songs, messages, blog postings, whatever). Each of these items is a full-fledged AR object.

My goal is to present the user with a simplified search, sort, and pagination interface. The user need not know that the Song has a singer, and that the Message has an author — to the end user both entries in the table will be displayed as “User”. Thus, the search box will simply be a dropdown list asking them which to search on (User name, created at, etc.). Internally, I would need to convert that to the appropriate object search, combine the results, and display.

I can, separately, do pagination (mislav will_paginate), sorting, and filtering, but together I’m having some problems combining them.

For example, if I paginate the combined list of items, the pagination plugin handles it just fine. It is not efficient since the pagination is happening in the app vs. the DB, but let’s assume the intended use-case would indicate the vast majority of the users will have less than 30 favorited items and all other behavior, server capabilities, etc. indicates this will not be a bottleneck.

However, if I wish to sort the list I cannot sort it via the pagination plugin because it relies on the assumption that the result set is derived from a single SQL query, and also that the field name is consistent throughout. Thus, I must sort the merged array via ruby, e.g.

@items.sort_by{ |i| i.whatever }

But, since the items do not share common names, I must first interrogate the object and then call the correct sort by. For example, if the user wishes to sort by user name, if the sorted object is a message, I sort by author but if the object is a song, I sort by singer. This is all very gross and feels quite un-ruby-like.

This same problem comes into play with the filter. If the user filters on the “parent item” (the message’s thread, the song’s album), I must translate that to the appropriate collection object method. Also gross.

This is not the exact set-up but is close enough. Note that this is a legacy app so changing it is quite difficult, although not impossible. Also, yes there is some DRY that can be done, but don’t focus on the style or elegance of the following code. Style/elegance of the SOLUTION is important, however! 😀

models:

class User < ActiveRecord::Base
  ...
  has_and_belongs_to_many :favorite_messages, :class_name => "Message"
  has_and_belongs_to_many :favorite_songs,    :class_name => "Song"
  has_many                :authored_messages, :class_name => "Message"
  has_many                :sung_songs,        :class_name => "Song"
end

class Message < ActiveRecord::Base
  has_and_belongs_to_many :favorite_messages  
  belongs_to              :author, :class_name => "User"
  belongs_to              :thread
end

class Song < ActiveRecord::Base
  has_and_belongs_to_many :favorite_songs  
  belongs_to              :singer, :class_name => "User"
  belongs_to              :album
end

controller:

def show
  u = User.find 123

  @items = Array.new
  @items << u.favorite_messages
  @items << u.favorite_songs
  # etc. etc. 

  @items.flatten!

  @items = @items.sort_by{ |i| i.created_at }

  @items = @items.paginate :page => params[:page], :per_page => 20
end

def search

  # Assume user is searching for username like 'Bob'

  u = User.find 123

  @items = Array.new
  @items << u.favorite_messages.find( :all, :conditions => "LOWER( author ) LIKE LOWER('%bob%')" )
  @items << u.favorite_songs.find( :all, :conditions => "LOWER( singer ) LIKE ... " )
  # etc. etc. 

  @items.flatten!

  @items = @items.sort_by{ |i| determine appropriate sorting based on user selection }

  @items = @items.paginate :page => params[:page], :per_page => 20
end

view:

   #index.html.erb
   ...
   <table>
     <tr>
       <th>Title (sort ASC/DESC links)</th>
       <th>Created By (sort ASC/DESC links))</th>
       <th>Collection Title (sort ASC/DESC links)</th>
       <th>Created At (sort ASC/DESC links)</th> 
     </tr>
     <% @items.each |item| do %>
       <%= render { :partial => "message", :locals => item } if item.is_a? Message %>
       <%= render { :partial => "song",    :locals => item } if item.is_a? Song %>
     <%end%>
   ...
   </table>

   #message.html.erb
   # shorthand, not real ruby
   print out message title, author name, thread title, message created at

   #song.html.erb
   # shorthand
   print out song title, singer name, album title, song created at
  • 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-05-13T09:54:58+00:00Added an answer on May 13, 2026 at 9:54 am

    Just as an FYI, I managed to accomplish all of this with a really fat model, but it seems to work quite nicely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 419k
  • Answers 419k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is that the system call creates yet another… May 15, 2026 at 10:28 am
  • Editorial Team
    Editorial Team added an answer The best (most generic) approaches will be to trick the… May 15, 2026 at 10:28 am
  • Editorial Team
    Editorial Team added an answer I can't speak for VB specifically, but I'm not aware… May 15, 2026 at 10:28 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.