Sql rails参数[:month]剥离前导零导致不正确匹配

Sql rails参数[:month]剥离前导零导致不正确匹配,sql,ruby-on-rails,ruby,Sql,Ruby On Rails,Ruby,在我的应用程序中,当用户访问financials/trends/2014/03时,将在我的trends\u控制器中执行选择查询 @expenses = Expense.select("name, amount, id, created_at").where(:user_id => current_user.id).where(["strftime('%Y', created_at) = ? AND strftime('%m', created_at) = ?", params[:year]

在我的应用程序中,当用户访问
financials/trends/2014/03
时,将在我的
trends\u控制器中执行选择查询

@expenses = Expense.select("name, amount, id, created_at").where(:user_id => current_user.id).where(["strftime('%Y', created_at) = ? AND strftime('%m', created_at) = ?", params[:year], params[:month] ])
@expenses_by_month = @expenses.group_by { |expense| expense.created_at.beginning_of_month }
如果查询返回结果,则呈现页面,否则将发生重定向。这一切似乎都很好

当我试图将
params[:month]
传递到索引页上的链接中时,会出现问题,:month中的前导零似乎被剥离,因此该链接将返回一个路径,例如
financials/trends/2014/3
,而不是select语句所需的
financials/trends/2014/03
(因为在db中,创建的_at值存储为2014-03-day)

路线:

get "finances/trends/:year/:month" => "trends#month", :as => :month_trends
index.html.erb:

<% @expenses_by_month.each do |month, expenses| %>
<%= link_to "#{month.strftime('%B')}", month_trends_path(:year => month.year, :month => month.month) %>
<% end %>

月.年,:月=>month.month)%>

我知道select语句中的%m应该以填充值的形式返回月份,所以我完全不确定是什么导致了这种情况。

如果可以的话,尝试一些直接的ruby方法。这里是如何

"3".rjust(2,"0") #=> 03
"3".ljust(2,"0") #=> 30

month=3
month.to_s.rjust(2,"0") #=> 03

这很有效。。当数字只有一位时,它会加上0,而当数字为两位时,它不会加上0。正是我需要的。