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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:31:09+00:00 2026-05-14T18:31:09+00:00

I have a table of events (in a sqlite3 database for what it’s worth)

  • 0

I have a table of events (in a sqlite3 database for what it’s worth) with a column called “when” that contains a timestamp detailing precisely when the event that particular row denotes is set to occur. Right now, I have

@events = Event.find(:all)

in my controller and I am using template helper methods to calculate where to place each event on my display page based on the day of the week it occurs on. For example:

<% if(event.when.wday == 6) %>
    # DO SOMETHING
<% end %>

I want to abstract this logic to the controller however. My idea was to do the following:

@thursday_events = Event.find(:all, :conditions => ["when.wday=4"])

Obviously (I guess?) this didn’t work. Throwing the error “SQLite3::SQLException: near “when”: syntax error: SELECT * FROM “events” WHERE (when.wday=4)”.

I’m assuming this is because I tried to use a helper method within a find condition but I don’t know a better way to do this. Any advice? 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-05-14T18:31:09+00:00Added an answer on May 14, 2026 at 6:31 pm

    The conditions parameter needs to be a fragment of SQL.

    :conditions => [“when.wday=4”]

    is a fragment of Ruby code, so no go.

    Try

    # Model Event has a datetime field named 'when'
    Event.find(:all, :conditions => ["strftime('%w', events.when) = 4"])
    

    SQLLite ref: http://www.sqlite.org/lang_datefunc.html

    Added:

    While more closely reading your post, I think you’re planning to send multiple instance variables (one per day of the week) from your controller to your view. That’s a good idea–moving logic out of the view. But, don’t do more dbms queries!

    Each query has significant overhead. Eg:

    #Do NOT do it this way (too many db queries)
    @sunday_events = Event.find(:all, 
       :conditions => ["strftime('%w', events.when) = 0"])
    @monday_events = Event.find(:all, 
       :conditions => ["strftime('%w', events.when) = 1"])
    @thursday_events = Event.find(:all, 
       :conditions => ["strftime('%w', events.when) = 4"])
    # ... etc
    
    # Better: Just 1 database query--
    events = Event.find(:all)
    @sunday_events   = events.select{|e| e.when.wday == 0}
    @monday_events   = events.select{|e| e.when.wday == 1}
    @thursday_events = events.select{|e| e.when.wday == 4} 
    # ... etc
    

    Final comment:

    Current best practice thinking is to move code into the models from controllers wherever reasonable. This is called “Fat model, skinny controller” In the above example, you could have a class-level method in the model create the individual instance variables. Or perhaps better, one hash that contains 7 values, each being an array of the records. Eg

    # in Event model
    def Event.find_by_day
      events = Event.find(:all)
      result = {}
      days = [:sun, :mon, :tue, :wed, :thu, :fri, :sat]
      (0..6).each{|day_i| result[days[day_i]] = 
                            events.select{|e| e.when.wday == day_i}
                  }
    
      result
    end
    
    # in controller
    @events = Event.find_by_day
    
    # in view
    # @events[:sun] is array of the Sunday events
    #   so do something with them...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Oracle table which contains event log messages for an application. We
I have a table of events with a recorded start and end time as
I have a table of events, I need to find all tail events of
I have an Events table whose goal is to store actions done by web
Suppose you have the tables Presentations and Events . When a presentation is saved
I have 2 tables event + event_artist event eventId | eventName ------------------- 1 ,
I have table inside a div tab. The table has 40 rows in it
I have table with 50 entries (users with such details like Name Surname Location
I have table rows of data in html being filled from a CGI application.
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this

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.