简体   繁体   中英

Rails list records for a particular day

I'm trying to create an index.html.erb file that will list records from a table called events. I'm trying to select records for a particular day. starts_at is a datetime field in the table. For example, I'm trying to get all of the records for Monday of this week using:

<% @events = Event.where('starts_at.to_date = ?', DateTime.now.beginning_of_week.to_date) each do |event| %>

But, I don't get records when I should.

One thing it doesn't like is the each statement.

Thanks in advance for the help!

我相信您缺少等号条件符号。

<% @events = Event.where('starts_at.to_date = ?', DateTime.now.beginning_of_week.to_date) each do |event| %>

You can use AREL.

@date = DateTime.now.beginning_of_week
@events = Event.where(starts_at: @date..@date.end_of_day)

And that should go in your controller. It's not recommended to do DB queries in your view (not very MVC!).

Event.find(:all, :conditions => ["DATE(starts_at) = DATE(?)", DateTime.now.beginning_of_week])

要么

Event.where("DATE(starts_at) = DATE(?)", DateTime.now.beginning_of_week)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM