[英]Very Basic Blog - how to set an instance variable to all the posts from a specific date from a datetime attribute in rails?
As the title says, I want to set an instanced variable equal to all the posts with the same date in a datetime attribute. 正如标题所说,我想设置一个实例变量,它等于datetime属性中具有相同日期的所有帖子。 I previously had two separate variables for date and time, but just recently restructured it so that it's a datetime and now I don't know how to parse through for the specific date.
我以前有两个单独的变量用于日期和时间,但最近刚重组它以便它是一个日期时间,现在我不知道如何解析特定日期。 Please advise.
请指教。 Here is the relevant code:
这是相关代码:
Post Controller: 后控制器:
def index
@datetime = DateTime.parse() rescue DateTime.now.to_date
@posts = Post.where("(I don't know what to put in here)").order('datetime ASC')
end
edit: schema 编辑:架构
create_table "posts", force: :cascade do |t|
t.string "title"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "datetime"
end
Assuming you have timestamps 假设你有时间戳
def index
target_date_time = Time.now.to_datetime
@posts = Post.where(:datetime => target_date_time.beginning_of_day..target_date_time.end_of_day).order('datetime ASC')
end
if you don't have timestamps always add them in the future and just put in the attribute where your date_time is stored in place of created_at. 如果你没有时间戳,总是在将来添加它们,只需将存储date_time的属性放在created_at的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.