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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:37:15+00:00 2026-06-01T09:37:15+00:00

My Discount model has one Period association. I am trying to write a scope

  • 0

My Discount model has one Period association. I am trying to write a scope that select discounts that starts today, this includes:

  1. when it has a period, select those periods that begins today.
  2. when it does not have a period, select those discounts which were created between yesterday and today

My current query can do the first requirement (actually a bit complex than this):

def self.begins_today
  joins(:event).where("begin = ?", today)
end

However how can I achieve requirement 2?

I was thinking of using SQL UNION command, but I think it can’t work as a scope.

  • 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-01T09:37:17+00:00Added an answer on June 1, 2026 at 9:37 am

    I’m assuming event contains the period association?

    In any case you want a left join between the discounts table and the periods table. This will give you the period data to do the begin = today where clause, and null if there is no period. Thus the SQL to select the data would be

    SELECT [columns]
    FROM discounts_table
    LEFT JOIN periods_table ON periods_table.discount_id = discounts_table.id
    WHERE (periods_table.begin = [today]) OR (periods_table.begin IS NULL AND discounts_table.created_at BETWEEN [yesterday] AND [today])
    

    in rails you should be able to achieve this as follows:

    Discount
      .joins("LEFT JOIN periods_table ON periods_table.discount_id = discounts_table.id")
      .where("(periods_table.begin = ?) OR (periods_table.begin IS NULL AND discounts_table.created_at BETWEEN ? AND ?)", today, today, 1.day.ago.to_date)
    

    Unfortunately you need the use SQL statements rather than letting rails create it for you as:

    1. joins with a symbol only creates an INNER JOIN, not a LEFT JOIN
    2. where with symbols, hashes etc will combine conditions using AND, not OR
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My discount class has a sales_period. I want to write a method which can
I'm trying to create a generic data model that will allow for a particular
I have a model like this. #models.py class Payment(models.Model): unit_price = models.DecimalField(max_digits=12, decimal_places=2) discount
I want to add coupon/discount code to custom asp.net site that already has a
In my model I have three complex scopes returning the Discount object arrays. Each
var main = (from d in db.Discounts where d.Id == discount.Id && d.UserId ==
Let's see if I can explain myself, I have this models: class BillHeader(models.Model): number
I'm trying to setup a discount for a the whole cart using PayPal +
I'm new on spree and I read that a product has a master variant
I would like to get Magento to tell me any discounts that a product

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.