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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:59:21+00:00 2026-06-01T11:59:21+00:00

I have an app where there is always a current contest (defined by start_date

  • 0

I have an app where there is always a current contest (defined by start_date and end_date datetime). I have the following code in the application_controller.rb as a before_filter.

def load_contest
 @contest_last = Contest.last
 @contest_last.present? ? @contest_leftover = (@contest_last.end_date.utc - Time.now.utc).to_i : @contest_leftover = 0

 if @contest_last.nil?
  Contest.create(:start_date => Time.now.utc, :end_date => Time.now.utc + 10.minutes)
 elsif @contest_leftover < 0
  @winner = Organization.order('votes_count DESC').first
  @contest_last.update_attributes!(:organization_id => @winner.id, :winner_votes => @winner.votes_count) if @winner.present?
  Organization.update_all(:votes_count => 0)
  Contest.create(:start_date => @contest_last.end_date.utc, :end_date => Time.now.utc + 10.minutes)
 end
end

My questions:

1) I would like to change the :end_date to something that signifies next Sunday at a certain time (eg. next Sunday at 8pm). Similarly, I could then set the :start_date to to the previous Sunday at a certain time. I saw that there is a sunday() class (http://api.rubyonrails.org/classes/Time.html#method-i-sunday), but not sure how to specify a certain time on that day.

2) For this situation of always wanting the current contest, is there a better way of loading it in the app? Would caching it be better and then reloading if a new one is created? Not sure how this would be done, but seems to be more efficient.

Thanks!

  • 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-01T11:59:23+00:00Added an answer on June 1, 2026 at 11:59 am

    Start with this snippet. In rails, given a date/time object d, the expression

    d - wday.day
    

    Gives the last Sunday not after that date. i.e. if the day itself is a Sunday the result will be d.

    Then to get the next sunday:

    sunday1 = d - wday.day
    sunday2 = sunday1 + 7.day
    

    And to set the times for both:

    sunday1 = (d - wday.day).change(:hour => 8,:min => 30)
    sunday2 = (sunday1 + 7.day).change(:hour => 20)
    

    No need for any external modules. Rails (but not core ruby!) has all the date/time manipulation functionality you need.

    Note how the change function works: if you pass it :hour only, it will reset the minute and second to 0. If you pass it :hour and :minute, it will reset only the second. See the docs for details.

    One last thing: be mindful of what time zone you are using.

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

Sidebar

Related Questions

I have an iPhone app out there that calls home to my server every
In my app there is requirement that..I have 6 buttons in a nib, when
I have an app, where there's a UITabBar with 5 tabs. When user shakes
I have an app, where there's one UIWebView and a UITableView. I don't want
I have a background that i put in my app and there are places
I have an app that has a TableView, NavigationView and TabBar running together. There
Are there any DHTMLX users out there? We have an app that is being
Is there an F# equivalent to eval? My intent is to have my app
I have a VB6 app. There is a main form. It houses a user
I have had this issue for sometime on my app and it always bothered

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.