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

  • Home
  • SEARCH
  • 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 4611670
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:17:56+00:00 2026-05-22T01:17:56+00:00

I have a model called RaceWeek. Two operations I want to perform: (1) I

  • 0

I have a model called RaceWeek. Two operations I want to perform:

(1) I want to scope-down the records using this condition:

"start_date <= #{Time.now} AND end_date >= #{Time.now} AND recordable_type != 'User'"

Then I want to delete all those records. Here’s how I am trying to now:

RaceWeek.delete_all("start_date <= #{Time.now} AND end_date >= #{Time.now} AND recordable_type != 'User'")

(2) After I delete all those, I then want to reset values for the remaining records. Here’s how I am trying to do that now:

RaceWeek.find(:all, :conditions => ["start_date <= #{Time.now} AND end_date >= #{Time.now}"]).update_all("games_won = 0, games_lost = 0")

Without going into all the issues that I’m running into, can you explain how you typically execute these type of operations?

  • 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-22T01:17:57+00:00Added an answer on May 22, 2026 at 1:17 am

    I think I see what you’re trying to do.. But I think there’s better ways you could approach it. I’m not sure of the context of what you’re doing so you may can replace the following named_scope names with something more appropriate, but if you have something like this:

    #Your RaceWeek Model
    class RaceWeek < ActiveRecord::Base
    
    named_scope :started_before_and_ended_after_today, :conditions => ["start_date <= #{Time.now} AND end_date >= #{Time.now}"]
    
    named_scope :not_recordable_user, :conditions => ["recordable_type != 'User'"]
    
    end
    

    Now in any of your controllers, instead of having to write out something like this each time:

    RaceWeek.find(:all, :conditions => ["start_date <= #{Time.now} AND end_date >= #{Time.now} AND recordable_type != 'User'"])
    

    You can just write something much cleaner like this:

    RaceWeek.started_before_and_ended_after_today.not_recordable_user.find(:all)
    

    So now you can chain named_scopes together whenever you like. But it looks like you’re trying to do something reasonably repetitive so rather than having to write that out every time in every controller you want to use it in you should just create a “class method” on your RaceWeek model to do it for you. The following model can be refractored down a lot more as well but hopefully this will get you started:

    #Your RaceWeek Model
    class RaceWeek < ActiveRecord::Base
    
    named_scope :started_before_and_ended_after_today, :conditions => ["start_date <= #{Time.now} AND end_date >= #{Time.now}"]
    
    named_scope :not_recordable_user, :conditions => ["recordable_type != 'User'"]
    
    def self.reset_records
      started_before_and_ended_after_today.not_recordable_user.delete_all   
      started_before_and_ended_after_today.each { |record| record.update_attributes(:games_won => 0, :games_lost => 0)}      
    end
    
    end
    

    Now whenever you need to delete everything and update only the remaining records that are left. All you need to do is call:

    RaceWeek.reset_records
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Business , and I want to save Business.all into
I have a model called Person. I want User to inherit from Person. class
I have a model called Details, and two controller methods new and create. New
Have a model called contact_email.date_sent I want to be able to run a report
I have a model called Vote and this is what vote.rb looks like: class
I have a model called Category in my app in GAE. This model simply
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have a model called Story that has two integer fields called views and
I have a model called user. I want to show the password field just
I have a model called Metadatum (silly, I know) which has two fields, key

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.