我一直在关注这篇文章here,试图为我的帖子设置档案。
我一直在努力,直到我试图创建一个循环来列出每个月内的每个帖子。
我收到此错误:
SQLite3::SQLException: near "from": syntax error: SELECT "bets".* FROM "bets" WHERE (extract(year from created_at) = '2016') AND (extract(month from created_at) = '2') ORDER BY created_at DESC
我的控制器看起来像这样:
class ResultsController < ApplicationController
def index
@bets_by_month = Bet.all.group_by { |bet| bet.created_at.beginning_of_month }
end
def by_year_and_month
@bets = Bet.where('extract(year from created_at) = ?', params[:year]).where('extract(month from created_at) = ?', params[:month]).order("created_at DESC")
end
end
我的by_year_and_month视图如下所示:
<%= @bets.each do |bet| %>
<% end %>
答案 0 :(得分:0)
这是你的问题:
'extract(year from created_at) = ?'
这种语法不正确。试试这个:
"strftime('%Y', created_at) = ?"