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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:14:23+00:00 2026-06-04T22:14:23+00:00

I have a rails app with three models: Account, Package and Delivery. I’m using

  • 0

I have a rails app with three models: Account, Package and Delivery. I’m using AR::Relation scopes on Delivery to find recent deliveries and returns. The Account model also needs to find accounts with recent deliveries. Can I use the same Delivery scopes in Account via joins? Right now I’m using code like this (note the duplication in Account.with_recent_returns and Delivery.recent_returns):

# Package(id: integer, ident: string)

class Package < ActiveRecord::Base
  has_many :deliveries, :dependent => :destroy
end

# Delivery(id: integer, account_id: integer, delivered_on: date, package_id: integer, returned_on: date)`

class Delivery < ActiveRecord::Base

  belongs_to :account
  belongs_to :package

  scope :current, where(:returned_on => nil)

  scope :recent_deliveries, where('delivered_on >= ?', 2.weeks.ago)

  scope :recent_returns, where('returned_on >= ?', 2.weeks.ago)

  scope :returns, where('returned_on IS NOT NULL')
end

#  => Account(id: integer, name: string)

class Account < ActiveRecord::Base

  has_many :deliveries, :dependent => :destroy

  def self.with_current_deliveries
    joins(:deliveries).where(:deliveries => { :returned_on => nil } ).includes(:deliveries)
  end


  def self.with_recent_returns
    joins(:deliveries).where('deliveries.returned_on >= ?', 2.weeks.ago).includes(:deliveries)
  end
end
  • 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-04T22:14:25+00:00Added an answer on June 4, 2026 at 10:14 pm

    I believe something like this will work

    scope :recent_deliveries, joins(:deliveries).merge(Delivery.recent)

    or perhaps you’ll need to use scoped

    scope :recent_deliveries, scoped.joins(:deliveries).merge(Delivery.recent)

    should get you started.

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

Sidebar

Related Questions

I have a rails app that requires multi-table inheritance for which I am using
Say I have a rails app with 3 models, Person, Place and Thing. Say
I have a Rails app acting as an OAuth 2.0 provider (using the oauth2-provider
I have a simple rails app with models project and phase. A project has
I have a Rails app with some basic models. The website displays data retrieved
I have a Rails 2.3 app with the following models. class Message << AR::Base
I have a rails app which currently has 3 models: Post , User and
I have a simple Rails 3 app: no models, one view and one controller
I have a naked rails 3 app with one model, generated using rails g
I have a rails app with two models, visits and users. A visit has

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.