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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:47:56+00:00 2026-06-02T14:47:56+00:00

ruby 1.9.2p290 rails 3.1.1 I have these models: class Recipe < ActiveRecord::Base belongs_to :festivity

  • 0
  • ruby 1.9.2p290
  • rails 3.1.1

I have these models:

class Recipe < ActiveRecord::Base
 belongs_to :festivity 
end

class Festivity < ActiveRecord::Base
 has_many :recipes 
end

I have the following field in the “recipes” table:

festivity_id

And the following datetime fields in the “festivities” table:

starts_at
ends_at

How to display the content based on festivities dates?

I started think in this static way:

class PagesController < ApplicationController
 def home
  @recipes_by_festivities = Recipe.where(:festivity_id => 4).all(:order => 'RAND()', :limit => 8)
 end
end

For example: In Xmas period (about all december month), I want to show a recipe list with the festivity_id = 1. In Thanksgiving period I wanna just a list of recipes with festivity_id = 4.

Am I clear? Let me know if I not.

SOLUTION

@current_festivity = Festivity.find(:last, :conditions => ["? between starts_at AND ends_at", Time.utc(0,Time.now.month,Time.now.day,0,0,0)])

@recipes_by_festivities = Recipe.where(:festivity_id => @current_festivity).all(:order => 'RAND()', :limit => 8)
  • 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-02T14:48:02+00:00Added an answer on June 2, 2026 at 2:48 pm

    This assumes starts_at and ends_at are saved with the same year (0), except if the period is in the middle of two years (Christmas time, for example): in that case, the ends_at year must be 1.

    Festivity.create(name: 'Christmas time', starts_at: Time.utc(0,12,25,0,0,0), ends_at: Time.utc(1,1,6,23,59,59))
    Festivity.create(name: 'Ferragosto', starts_at: Time.utc(0,8,15,0,0,0), ends_at: Time.utc(0,8,15,23,59,59))
    
    Recipe.create(festivity: Festivity.find_by_name('Periodo natalizio'))
    Recipe.create(festivity: Festivity.find_by_name('Ferragosto'))
    
    # Searching for festivities today
    Recipe.includes(:festivity).where(['? BETWEEN festivities.starts_at AND festivities.ends_at', Time.utc(0,Time.now.month,Time.now.day,12,0,0)]).all
    # Searching for festivities on 15 August (in Italy there is a festivity that is called Ferragosto)
    Recipe.includes(:festivity).where(['? BETWEEN festivities.starts_at AND festivities.ends_at', Time.utc(0,8,15,12,0,0)]).all
    # Searching for festivities on 28 December
    Recipe.includes(:festivity).where(['? BETWEEN festivities.starts_at AND festivities.ends_at', Time.utc(0,12,28,12,0,0)]).all
    

    And this is a possible implementation of a function:

    def recipes_on(day, month)
      Recipe.includes(:festivity).where(['? BETWEEN festivities.starts_at AND festivities.ends_at', Time.utc(0,month,day,12,0,0)]).all
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ruby 1.9.2p290 rails 3.1.1 Basically I have two models: CHEFS and RECIPES. class Chef
ruby 1.9.2p290 rails 3.1.1 I have two models: RECIPE and RECIPE_CATEGORY with belongs_to and
My environment: Ruby 1.9.2p290, Rails 3.0.9 and RubyGem 1.8.8 unfortunately I have an issue
Somehow my rails installation hides itself successfully... I have: ruby 1.9.2p290 (2011-07-09 revision 32553)
I'm using Rails 3.0.0 + passenger + apache2 running on Ruby 1.9.2p290 on an
I have an app based on Rails 3 (version Rails 3.1.2 ) and ruby
I am running Ruby on Rails 3.2.2 from 3.1.0. I have the issue undefined
Ruby on Rails controllers will automatically convert parameters to an array if they have
I'm running Ruby 1.9.2p290 and Rails 3.1.0.rc5. When I run this regex it matches
I have been using rvm and when I installed ruby 1.9.2, it installed ruby-1.9.2-p290

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.