Ruby on Rails:如何对重复时间(每周活动)进行建模?
在我的 Ruby on Rails 应用程序中,我有一个“组”模型,每周都会发生“活动”。有些活动每周仅发生一天(周五下午 6:00),而有些活动则每周发生多次(周一至周五上午 8:00,或周二/周四上午 10:00)。
我在尝试弄清楚如何对这些数据进行建模以及如何使用 Rails 创建表单来创建/更新数据时遇到困难。我是否创建一个包含日期时间字段的“活动”表?或者我是否可以将一周中的某一天与一天中的时间分成两个单独的字段?那么每周进行多次的活动又如何呢?
任何想法或建议将不胜感激。另外,如果您知道可以帮助解决此问题的 Gem,我将不胜感激,这样我就不必重新发明轮子。
更新:
例如,如果我需要显示如下内容:
特殊组 A 的活动
- 周一晚上 10 点 - 足球
- 周二/周四上午 8 点 - 网球
特别组 B 的活动
- 周一至周五中午 12 点 - 午餐
- 周六上午 8 点 - 早餐,
- 周日下午 6 点 - 晚餐
为了使用 Ruby on Rails 建模和显示此数据,我需要采取哪些步骤?
In my Ruby on Rails application, I have a "Group" model that has weekly recurring "activities". Some activities occur only one day a week (Friday at 6:00pm) while some occur multiple times a week (Mon-Fri at 8:00am, or Tues/Thurs at 10:00am).
I am having trouble trying to figure out how to model this data, and how to use Rails to create a form to create/update the data. Do I create an "Activities" table that has a datetime field? Or do I separate the day of the week from the time of day into two separate fields? What about the activities that occur multiple times a week?
Any ideas or advice would be appreciated. Also, I would appreciate knowing if you know of a Gem that helps with this so I don't have to re-invent the wheel.
Update:
For Example, if I needed to display something like this:
Special Group A's Activities
- Monday at 10pm - Football
- Tues/Thurs at 8am - Tennis
Special Group B's Activities
- Monday-Friday at 12pm - Lunch
- Saturday at 8am - Breakfast
- Sunday at 6pm - Dinner
What steps would I need to take in order to model and display this data, using Ruby on Rails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
型号
group.rb
迁移(单独或全部一起)
add_everything.rb
负责处理您的模型工作。在您的 groups_form 视图中,我将添加您关联的组名称、fields_for 作为您的活动名称和 fields_for 发生次数。在您的事件中,使用这个方便的 jQuery 日期时间选择器(它是 jQuery 日期选择器的扩展)来填充您的事件字段:
http://puna.net.nz/timepicker.htm
您还应该有单独的视图,以使用其各自的表单单独管理活动。在您的显示页面中显示其他字段是非常标准的,但是对于某些情况,您可以使用类似(haml 语法)的内容:
希望这至少可以让您开始。如果您想显示第 1 天/第 2 天/第 3 天的内容,您可以添加额外的逻辑来检查 Activity.occurrences.size 以进行相应的格式化
Models
group.rb
Migrations (separate or all together)
add_everything.rb
That take's care of your model work. In your groups _form view I would add your associated group name, and fields_for for your activity name and fields_for occurance. In your occurance, use this handy jQuery datetime picker that is an extension off of the jQuery date picker, to populate your occurance field:
http://puna.net.nz/timepicker.htm
You should also have separate views to manage activities separately with it's own respective form. In your show page displaying other fields is pretty standard, but for the occurances you can have something like (haml syntax):
Hope this at least gets you started. You can add additional logic for checking activity.occurances.size to format accordingly if you want to display something day1/day2/day3
https://github.com/jimweirich/texp Jim Weirich 的时间表达式库是查询这些内容的绝佳资源红宝石中的各种东西。
如果您除了批量查看所有集合之外不需要查询该集合,那么只需序列化该数据结构可能就适合您。
但最终您可能只使用
has_many :occurrences
,其中事件以 date_time homebaked-recurrence-pattern 对开始,并根据您需要的功能集从那里进行迭代。https://github.com/jimweirich/texp Jim Weirich's Temporal expressions library is an excellent resource for querying these sorts of things in ruby.
If you don't need to query this set other than looking at all of them in batch, then just serializing that datastructure would probably work for you.
But in the end you'll probably just use a
has_many :occurances
where occurances start off as date_time homebaked-recurrence-pattern pairs and iterate from there depending on what feature set you need.如果您直接思考,您有很多可能性可以在数据库上表示和保留不同的日期和时间格式和间隔,而且您可能会更改它以向用户添加一些选项或相应地删除选项您网站的成长。
我会创建两个字段,例如“date_start”和“date_end”,以及一个包含表示频率的代码的字符串字段。每个代码 3 个字符,第一个可以是 M 每月,W 每周;第二个可以是 F 代表第一个,L 代表最后一个;第三个字符可以是表示一周中特定数字的数字,F 表示星期五。
这里的要点是,您可以以编程方式(编码和)解码该内容,因此如果您添加功能,则无需重新创建数据库关系。
If you think it straight, there is a great number of possibilites for you to represent and persist diverse date and time formats and intervals on a database, also you probably are going to change it to add some options to users or to remove options accordingly to the growth of your website.
I would go with creating two fields like "date_start" and "date_end", and one string field containing a code that represents the frequency. Something like 3 chars per code, first can be M for monthly, W weekly; second can be F for first, L for last; third char can be a number for a specific number of the week, F for friday.
The point here is that you can (encode and) decode that programatically so if you add features you won't have to recreate your database relations.
就像一个小组可以进行多项活动一样,我认为一项活动也可以多次发生。我建议尝试使用出现表以这种方式对数据库进行建模。
就形式而言,以活动为主、事件为细的“主-细”形式怎么样?
In the same way a group can have multiple activities, I think an activity can have multiple occurances. I would suggest trying to model your database that way, with a occurance table.
Regarding the form, what about a "master - detail" form with activity as the master, and occurance as the detail?