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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:35:23+00:00 2026-06-09T06:35:23+00:00

I have three models ie image, datafiles and video. I want to fetch the

  • 0

I have three models ie image, datafiles and video. I want to fetch the latest content by a user from all the three models.

One very bad of doing this can be:

  images = User.find(8).images
  videos = User.find(8).videos
  data_files = User.find(8).data_files

All the content:

 all_content = images + videos + data_files
 all_content.sort_by(&:created_at).reverse.paginate(:page => params[:page],:per_page=> per_page)

How can do this in just one query to make the code better.

  • 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-09T06:35:24+00:00Added an answer on June 9, 2026 at 6:35 am

    Probably it’s a good idea to have a Content model which connects a user to his contents. Something like this:

    class User < ActiveRecord::Base
      has_many :contents
    end
    
    class Content < ActiveRecord::Base
      belongs_to :user
      # Remember the contentable_type and contentable_id columns
      belongs_to :contentable, :polymorphic => true
    end
    
    # Similar relations for Image/File models
    class Video < ActiveRecord::Base
      has_one :content, :as => :contentable
      belongs_to :user, :through => :content
    end
    
    # Then to fetch the last contents from the user
    Content.where(:user_id => 8).order('created_at DESC')
           .paginate(:page => params[:page], :per_page=> per_page)
    

    It’s just a proof of concept, you could even go with single table inheritance if you prefer, it depends on the attributes each of your content model has, probably it’s the better thing given these object usually contains a title, a body, a link to the content, an url and similar stuff and few differences.

    If you go with STI you need to define a base model (Content in this case) with the common attributes and several other model for each of your content model (e.g. Image or Video). These models inherit from the Content model. Remember to have a contents table in your DB with a type column (which will contain the string with the type of content, e.g. ‘Video’) and all the columns for the child models (yes, if you use this approach all the data will go in a single table).

    UPDATE: The Single Table Inheritance approach has several pros, e.g. you get all the contents’ data in one query, the previous solution is good for sorting but you still need to get the Video, and other specific data with other queries:

    # The contents table has a type string column and
    # all the specific models's columns
    class Content < ActiveRecord::Base
    end
    
    # Video class inherit from Content (a Video is a Content!)
    class Video < Content
    end
    

    In this case if you want all the user’s content you can use user.contents and you’ll get an array of the specific objects (Video, Image and so on) depending of their type.
    You can also get just the images or videos with something like this Video.all

    Of course these are just ideas, the real implementation depends on your problem and requirements.

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

Sidebar

Related Questions

I have three models: User Image Group . User belongs to an image; Group
From the image below. It can be seen that I have three table User,
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have three models that are coming together to create one view model and
I have three models: Post , Comment , User and Vote . I'm using
I have three models that I want to interact with each other. Kase, Person
I have two user models one is 1. User.rb (this is my own authentication
I have three models that I'm trying to setup: Location/Venues, Categories, and Neighborhoods. A
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <

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.