rails 如何使用对象显示的时间段?
how rails use time period for object to display?
我正在我的 rails 应用程序中创建一些 类别。我想根据时间设置的不同周期来决定文章什么时候显示。我正在寻找一种更好的方法来做到这一点,因为我不认为在这种情况下需要使用 gem 。
我想请教的问题是:
1.how to set time period for Category
2.how to check the time period(maybe use between?) when conducting query,
3.all schedules can be displayed like a google calendar(use simple_calendar), the entire schedule will be showed in my backstage.
我首先想到的解决方案是在类别中创建两个日期列并定义开始时间和结束时间。
有没有更好的方法来处理这个问题?有没有专门针对这个目的或者这种情况的gem?
如果我理解正确的话,Category
是用户可以动态创建的时间段。将有一些不同的 Articles
也会有与之关联的时间段。如果 Articles's
时间落在 Category
的开始和结束时间之内,则 Articles
将落入 Category
。如果是这种情况,时间段必须有开始和结束时间。类似于以下内容:
# Category Class
class Category < ActiveRecord::Base
# Omitting rest of the class..
def articles_created_between(time_range)
articles.where(created_at: time_range)
end
end
然后您可以加载一个类别:
category = Category.last
并找到类别中的文章:
articles = category.articles_created_between((Time.now.midnight - 1.day)..Time.now.midnight)
查看远程条件了解更多信息:
http://guides.rubyonrails.org/active_record_querying.html#hash-conditions
我正在我的 rails 应用程序中创建一些 类别。我想根据时间设置的不同周期来决定文章什么时候显示。我正在寻找一种更好的方法来做到这一点,因为我不认为在这种情况下需要使用 gem 。
我想请教的问题是:
1.how to set time period for Category
2.how to check the time period(maybe use between?) when conducting query,
3.all schedules can be displayed like a google calendar(use simple_calendar), the entire schedule will be showed in my backstage.
我首先想到的解决方案是在类别中创建两个日期列并定义开始时间和结束时间。
有没有更好的方法来处理这个问题?有没有专门针对这个目的或者这种情况的gem?
如果我理解正确的话,Category
是用户可以动态创建的时间段。将有一些不同的 Articles
也会有与之关联的时间段。如果 Articles's
时间落在 Category
的开始和结束时间之内,则 Articles
将落入 Category
。如果是这种情况,时间段必须有开始和结束时间。类似于以下内容:
# Category Class
class Category < ActiveRecord::Base
# Omitting rest of the class..
def articles_created_between(time_range)
articles.where(created_at: time_range)
end
end
然后您可以加载一个类别:
category = Category.last
并找到类别中的文章:
articles = category.articles_created_between((Time.now.midnight - 1.day)..Time.now.midnight)
查看远程条件了解更多信息: http://guides.rubyonrails.org/active_record_querying.html#hash-conditions