Using Rails Helper Methods within ActionRecord Queries?
Posted
by Pygmalion
on Stack Overflow
See other posts from Stack Overflow
or by Pygmalion
Published on 2010-05-03T02:28:48Z
Indexed on
2010/05/03
2:38 UTC
Read the original article
Hit count: 351
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!
© Stack Overflow or respective owner