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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:04:41+00:00 2026-06-14T00:04:41+00:00

I have 3 models: Class Model1 end Class Model2 end Class Model3 end I

  • 0

I have 3 models:

Class Model1

end

Class Model2

end

Class Model3

end

I have this code:

scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_time...end_time)) }

class << self
 ## Class methods for calculating searches
 def created_today
  today = Time.zone.now
  created_between(today.beginning_of_day, today.end_of_day)
 end

 def created_yesterday
  yesterday = Time.zone.now - 1.day
  created_between(yesterday.beginning_of_day, yesterday.end_of_day)
 end

 def created_last_week
  start_time = (Time.zone.now - 1.week).beginning_of_day
  end_time = Time.zone.now
  created_between(start_time, end_time)
 end

 def created_last_month
  start_time = (Time.zone.now - 1.month).beginning_of_day
  end_time = Time.zone.now
  created_between(start_time, end_time)
 end

 def created_last_year
  start_time = (Time.zone.now - 1.year).beginning_of_day
  end_time = Time.zone.now
  created_between(start_time, end_time)
 end

end

I need add this code to 3 models but I don’t want repeat the code inside each model.

How can I do it?

Thank you very much!

  • 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-14T00:04:42+00:00Added an answer on June 14, 2026 at 12:04 am

    Try this:

    module CommonClassFunctions
    
     def self.included(base)
       base.send :extend, ClassMethods
     end
    
     module ClassMethods
       ## Class methods for calculating searches
       def created_today
        today = Time.zone.now
        created_between(today.beginning_of_day, today.end_of_day)
       end
    
       def created_yesterday
        yesterday = Time.zone.now - 1.day
        created_between(yesterday.beginning_of_day, yesterday.end_of_day)
       end
    
       def created_last_week
        start_time = (Time.zone.now - 1.week).beginning_of_day
        end_time = Time.zone.now
        created_between(start_time, end_time)
       end
    
       def created_last_month
        start_time = (Time.zone.now - 1.month).beginning_of_day
        end_time = Time.zone.now
        created_between(start_time, end_time)
       end
    
       def created_last_year
        start_time = (Time.zone.now - 1.year).beginning_of_day
        end_time = Time.zone.now
        created_between(start_time, end_time)
       end
      end
    end
    
    Class Model1
      include CommonClassFunctions
    end
    
    Class Model2
      include CommonClassFunctions
    end
    
    Class Model3
      include CommonClassFunctions
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
i have two models: class Category has many :jobs end class Job belongs_to :category
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have 3 models class User < ... belongs_to :language has_many :posts end class
I have three models: class A < ActiveRecord::Base has_many :bs end class B <
I have the following models: class Foo has_and_belongs_to_many :bars end class Bar has_and_belongs_to_many :foos
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
I have this code: class Company < ActiveRecord::Base has_many :users, :through => :company_users do
I have two models: class Sentence < ActiveRecord::Base attr_accessible :sentence_id, :authority_name #... end class

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.