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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:35:43+00:00 2026-06-05T18:35:43+00:00

I currently have the following controller method in a Rails app: def index @entries

  • 0

I currently have the following controller method in a Rails app:

def index
  @entries = []
  @entries << QuickPost.where(:user_id.in => current_user.followees.map(&:ff_id) << current_user.id)
  @entries << Infographic.where(:user_id.in => current_user.followees.map(&:ff_id) << current_user.id)
  @entries.flatten!.sort!{ |a,b| b.created_at <=> a.created_at }

  @entries = Kaminari.paginate_array(@entries).page(params[:page]).per(10)
end

I realise this is terribly inefficient so I’m looking for a better way to achieve the same goal but I’m new to MongoDB and wondering what the best solution would be.

Is there a way to make a sorted limit() query or a MapReduce function in MongoDB across two collections? I’m guessing there isn’t but it would certainly save a lot of effort in this case!

I’m currently thinking I have two options:

  1. Create a master ‘StreamEntry’ type model and have both Infographic and QuickPost inherit from that so that both data types are stored on the same collection. The issue with this is that I have existing data and I don’t know how to move it from the old collections to the new.
  2. Create a separate Stream/ActivityStream model using something like Streama (https://github.com/christospappas/streama). The issues I can see here is that it would require a fair bit of upfront work and due to privacy settings and editing/removal of items the stream would need to be rebuilt often.

Are there options I have overlooked? Am I over-engineering with the above options? What sort of best practices are there for this type of situation?

Any info would be greatly appreciated, I’m really liking MongoDB so far and want to avoid falling into pitfalls like this in the future. Thanks.

  • 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-05T18:35:44+00:00Added an answer on June 5, 2026 at 6:35 pm

    The inherit solution is fine, but when the inherited models are close.
    For example :

    class Post < BasePost
      field :body, type: String
    end
    class QuickPost < BasePost
    end
    class BasePost
      field :title, type: String
      field :created_at, type: Time
    end
    

    But when the models grows, or are too different, your second solution is better.

    class Activity
      include Mongoid::Document
    
      paginates_per 20
    
      field :occurred_at, :type => Time, :default => nil
      validates_presence_of :occurred_at
    
      belongs_to :user
      belongs_to :quick_post
      belongs_to :infographic
    
      default_scope desc(:occurred_at)
    end
    

    and for example :

    class QuickPost
      include Mongoid::Document
      has_one :activity, :dependent => :destroy
    end
    

    The dependant destroy make the activity destroyed when the QuickPost is destroyed. You can use has_many and adapt.

    And to create the activities, you can create an observer :

    class ActivityObserver < Mongoid::Observer
      observe :quick_post, :infographic
    
      def after_save(record)
        if record.is_a? QuickPost
          if record.new_record?
            activity = record.build_activity
            activity.user = record.user
            # stuff when it is new
          else
            activity = record.activity
          end
          activity.occurred_at = record.occurred_at
          # common stuff
          activity.save
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have the following Mapping in my Controller: @RequestMapping( value = /add.html, method
I have the following application_controller method: def current_account @current_account ||= Account.find_by_subdomain(request.subdomain) end Should I
I currently have the following row in my table: course_data: user_id days <-- This
I have a Rails app with a Route resource, and a Route controller (not
I'm upgrading an app to Rails 3. We have an Application Controller like the
How can I use exception handling in Rails? Currently I have done following. In
I have currently the following routes: routes.IgnoreRoute({resource}.axd/{*pathInfo}); routes.IgnoreRoute({resource}.gif/{*pathInfo}); MvcRoute.MappUrl({controller}/{action}/{ID}) .WithDefaults(new { controller = home,
Suppose I have the following view in my rails app <% if is_subscriber? %>
I currently have the following CSS for my 4 columns which might grow to
I currently have the following objects persisting successfully: Person first name, etc. Exams title,

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.