English 中文(简体)
今年的铁路3记录
原标题:Rails3 records this year

In Rails3 how do I retrieve all records that have a date of this year? I d like to retrieve all my "Projects" where the "date" is this year.

我怀疑那会跟...

@projects_this_year = Project.where(:date.Time.year == Time.now.year)
or
@projects_this_year = Project.where(:date >= ?, Time.now.year)

我在看医生 但还没弄清楚

最佳回答

你可以做这样的事情(假设是 MySQL DB):

或更好:

不需要装入时间库 。

参考文献:http://dev.mysql.com/doc/refman/5.1/en/date-and-time-offices.html

编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑编辑


如果您想要独立数据库, 您可以做类似的事情 :

问题回答

如果您经常需要特定的月份或年份, 或者通过扩展@David s 回答的参数, 也可以在模型中做出这种动态 :

def self.get_year(date)
    # sanitize if params
    safe = [ 2011 ,  2012 ,  2013 ]
    if safe.include?(date)
         where("year(created_at) = ?", Time.parse("#{date}-01-01 11:11:11 -0500").year)
    else
         scoped
    end
end

特例 :

Product.get_year( 2012 )

NOTE 使这在隐秘上发挥作用,请更改以下内容:

where("date_part( year , created_at) = ?", Time.parse("#{date}-01-01 11:11:11 -0500").year

http://www.java2s.com/Code/PostgreSQL/Date-Timezone/datepart yeardate.htm" rel=“no follow” >http://www.java2s.com/Code/PostgreSQL/Date-Timezone/datepartyyyeardate.htm





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签