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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:52:06+00:00 2026-05-25T05:52:06+00:00

I using Rails 3 and SQLite and I am trying to make an active

  • 0

I using Rails 3 and SQLite and I am trying to make an active record query and order the results at the same time using information from another model. I have a resources model(which belongs to user) and a shared_items model to handle users sharing resources with each other. I would like to create a feed of all the resources that a user has created and which have been shared with them. The resource model with a working query:

class Resource < ActiveRecord::Base
  has_many :shared_items, :dependent => :destroy

  default_scope :order => 'resources.shared_items.created_at DESC'
  scope :from_users_sharing_with, lambda { |user| shared_with(user) }

  private

  def self.shared_with(user)

  resource_ids = %(SELECT resource_id FROM shared_items WHERE shared_with_id = :user_id)
  where("id IN (#{resource_ids}) OR user_id = :user_id",
        { :user_id => user })
end

This query creates a nice ‘feed’ array of all the resources that have been shared with or created by the user but they are ordered by the resource create date rather than creation date of the corresponding shared_item which is what I am looking to do. How can I write the ordering at the database level? I thought I could do the ordering in Ruby (like Ian suggests here) but as I am wanting fetch records 25 at a time in pagination, I don’t think I can take that option.
Any thoughts appreciated.

  • 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-25T05:52:06+00:00Added an answer on May 25, 2026 at 5:52 am
    class Resource < ActiveRecord::Base
      has_many :shared_items, :dependent => :destroy
    
      scope :from_users_sharing_with, lambda { |user| 
        includes(:shared_item).
          where("shared_items.shared_with_id = ? OR resources.user_id = ?", user.id, user.id).
          order("shared_items.created_at DESC")
        # might need to add `select("DISTINCT resources.*")` at the end 
      }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

rails 2.3.4, sqlite3 I'm trying this Production.find(:all, :conditions => ["time > ?", start_time.utc], :order
I'm using Active Record outside of rails and I have two AR classes similar
Hi I'm using the rails plugin acts-as-taggable-on and I'm trying to find the top
Using Rails I'm trying to get an error message like The song field can't
I am trying to set up Ruby on Rails on windows. I am using
I am working on a rails application, and I am using Sqlite in my
I am just trying to walk through this tutorial http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html#redis_and_rails And when I put
I am using Ruby on Rails 3.0.9. I am trying to get the list
Am trying out Ruby on Rails in Windows XP using the Hello World example.
I'm trying to initiate a Rails 3 console from Netbeans 6.9.1 (just upgraded) and

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.