在使用date_select并在Rails中放弃年份时按顺序保存日期? [英] Keeping dates in order when using date_select and discarding year in Rails?
问题描述
我的应用拥有使用季节性产品的用户。当用户选择产品时,我们允许他也选择产品的季节。我们通过让他选择每个产品的开始日期和结束日期来实现这一点。
我们使用 date_select
生成两组下拉菜单:一个用于开始日期,另一个用于结束日期。
包括年份对我们的模型没有意义。所以我们使用这个选项: discard_year =>真正的
为了解释我们的问题,请考虑我们的产品是苹果。供应商X每年从9月份到1月份运送苹果。这里的年份并不相关,这就是为什么我们使用 discard_year =>真
。然而,虽然具体年份不相关,但从开始日期到结束日期的相对时间点是相关的。这是我们问题出现的地方。
当您使用 discard_year => true
,Rails在数据库中设置了一年,它不会出现在视图中。 Rails将我们应用程序中的所有年份都设置为0001。回到我们的苹果示例,这意味着数据库现在认为用户已选择0001年9月到0001年1月。由于多种原因,这对我们来说是一个问题。
为了解决这个问题,我需要实现的逻辑如下: 如果season_start月/日在season_end之前数字化但是,如果season_start每月/每天在season_end month / day之后以数值表示,那么我需要动态更新(每月/每天),那么标准Rails方法就可以了。
我最好的猜测是我会创建一个自定义方法,它以 after_save
或 after_update
在我的产品模型中。但我不确定如何做到这一点。
想法?有人有过这个问题吗?谢谢!
before_save
c> Product model。 class Product< ActiveRecord :: Base
before_save:fix_end_date
def fix_end_date
self.end_date + = 1.year if start_date> end_date
结束
结束
My app has users who have seasonal products. When a user selects a product, we allow him to also select the product's season. We accomplish this by letting him select a start date and an end date for each product.
We're using date_select
to generate two sets of drop-downs: one for the start date and one for the end date.
Including years doesn't make sense for our model. So we're using the option: discard_year => true
To explain our problem, consider that our products are apples. Vendor X carries apples every year from September to January. Years are irrelevant here, and that's why we're using discard_year => true
. However, while the specific years are irrelevant, the relative point in time from the start date to the end date is relevant. This is where our problem arises.
When you use discard_year => true
, Rails does set a year in the database, it just doesn't appear in the views. Rails sets all the years to 0001 in our app. Going back to our apple example, this means that the database now thinks the user has selected September 0001 to January 0001. This is a problem for us for a number of reasons.
To solve this, the logic that I need to implement is the following:
If season_start month/day is numerically before season_end month/day, then standard Rails approach is fine.
But, if season_start month/day is numerically AFTER season_end month/day, then I need to dynamically update the database field such that the year for season_end is equal to the year for season_start + 1. For example, if a user selects October 15 to January 15, what he intends is October 15, 0001 to January 15, 0002. I need the app to reflect this.
My best guess is that I would create a custom method that runs as an after_save
or after_update
in my products model. But I'm not really sure how to do this.
Ideas? Anybody ever had this issue? Thanks!
Add a before_save
callback in your Product
model.
class Product < ActiveRecord::Base
before_save :fix_end_date
def fix_end_date
self.end_date +=1.year if start_date > end_date
end
end
这篇关于在使用date_select并在Rails中放弃年份时按顺序保存日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!