Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Rails 4按月份和年份进行查询排序_Sql_Ruby On Rails_Ruby - Fatal编程技术网

Sql Rails 4按月份和年份进行查询排序

Sql Rails 4按月份和年份进行查询排序,sql,ruby-on-rails,ruby,Sql,Ruby On Rails,Ruby,我很难找到最好的方法来做到这一点,但实际上我有一个博客,我想显示所有已发布博客的月份和年份档案。点击“2012年5月”链接,您将看到同一年该月之间撰写的所有博客(例如:2012年5月5日、2012年5月20日……)。是否有一种方法可以仅使用创建的_at并传递数字月份和年份的参数来获取同一年同一月份内的所有记录?总体而言,有没有更好的方法可以做到这一点。是的,您可以在Rails中做到这一点,例如2014年5月的所有博客: time = DateTime.new(2014,5) blogs = Bl

我很难找到最好的方法来做到这一点,但实际上我有一个博客,我想显示所有已发布博客的月份和年份档案。点击“2012年5月”链接,您将看到同一年该月之间撰写的所有博客(例如:2012年5月5日、2012年5月20日……)。是否有一种方法可以仅使用创建的_at并传递数字月份和年份的参数来获取同一年同一月份内的所有记录?总体而言,有没有更好的方法可以做到这一点。

是的,您可以在Rails中做到这一点,例如2014年5月的所有博客:

time = DateTime.new(2014,5)
blogs = Blog.where("created_at >= ? and created_at <= ? ", time.beginning_of_month, time.end_of_month)
time=DateTime.new(2014,5)

blogs=Blog.where(“created_at>=”和created_at是的,您可以在Rails中这样做,例如,对于2014年5月的所有Blog:

time = DateTime.new(2014,5)
blogs = Blog.where("created_at >= ? and created_at <= ? ", time.beginning_of_month, time.end_of_month)
time=DateTime.new(2014,5)

blogs=Blog.where(“created_at>=”和created_at在控制器中尝试以下操作:

month = Date.new(params[:year], params[:month])
@entries = Blog.where(created_at: month.all_month)

#all_month
方法将创建一个包含月初和月底的范围,并在where子句中传递一个范围,从而对日期进行BEVER查询。

在控制器中尝试以下操作:

month = Date.new(params[:year], params[:month])
@entries = Blog.where(created_at: month.all_month)

#all_month
方法将创建一个包含月初和月底的范围,并在where子句中传递一个范围,对日期进行一个BEVER查询。

在Rails 4.1中是
all_month
新的吗?就在Rails 4.1.4中,它可以工作,最简洁的回答是在3.2和4.0中的
Time
类中,所以它可能被移动了大约有一点。Rails 4.1中的
all__month
是新的吗?就在Rails 4.1.4中,最简洁的回答是它在3.2和4.0中的
Time
类中,所以它可能会移动一点。这可能应该是
之间?和?
因为我认为你跳过了这里的最后一天,因为
应该是
之间?还有?
因为我觉得你带着