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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:56:21+00:00 2026-05-14T03:56:21+00:00

If i have two tables Books, CDs with corresponding models. I want to display

  • 0

If i have two tables Books, CDs with corresponding models.

I want to display to the user a list of books and CDs. I also want to be able to sort this list on common attributes (release date, genre, price, etc.). I also have basic filtering on the common attributes.

The list will be large so I will be using pagination in manage the load.

items = []
items << CD.all(:limit => 20, :page => params[:page], :order => "genre ASC")
items << Book.all(:limit => 20, :page => params[:page], :order => "genre ASC")
re_sort(items,"genre ASC")

Right now I am doing two queries concatenating them and then sorting them. This is very inefficient. Also this breaks down when I use paging and filtering. If I am on page 2 of how do I know what page of each table individual table I am really on? There is no way to determine this information without getting all items from each table.

I have though that if I create a new Class called items that has a one to one relationship with either a Book or CD and do something like

Item.all(:limit => 20, :page => params[:page], :include => [:books, :cds], :order => "genre ASC")

However this gives back an ambiguous error. So can only be refined as

Item.all(:limit => 20, :page => params[:page], :include => [:books, :cds], :order => "books.genre ASC")

And does not interleave the books and CDs in a way that I want.

Any suggestions.

  • 1 1 Answer
  • 1 View
  • 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-14T03:56:21+00:00Added an answer on May 14, 2026 at 3:56 am

    You say you can’t change how books and CDs are stored in the database, but could you add an items view? Do you have any control over the database at all?

    CREATE VIEW items
      (id, type, title, genre, created_at, updated_at)
      AS
      SELECT b.id, 'Book', b.title, b.genre, b.created_at, b.updated_at
        FROM books b
      UNION
      SELECT c.id, 'CD', c.title, c.genre, c.created_at, c.updated_at
        FROM cds c;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is for a library management system. I have two tables, one is Books
I have two tables: books (id, name, desc, instance_id) instances (id, domain) A user
Let's say I have two tables of books and two tables of their corresponding
I have two tables: author (id, first_name, last_name) books (id, title, rate, author_id) and
It's easier with an example. I have two tables: Books and Chapters. Both have
I have two tables, Books and Authors , with many-to-many relationship between them through
I have two tables: connections id | publisherId | authorId and books id |
I'm fine with both C# and VB.NET I have two tables. Authors and Books.
I have two tables, books and authors . books has a author_id column and
I have two tables like below, mysql> select * from Books ; +----+------+------------+----------+----------+ |

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.