在 Rails 3 模型中,DateTime 属性实际上是“脏”属性。
在我的 Rails 3 应用程序中,如果实际对模型进行了更改,我只想在某个日志中写入一个条目。因此,如果用户不更改任何字段并单击“提交”,则不应有日志条目。
但似乎无论如何,Rails 似乎总是认为模型的 DateTime 属性已被更改。
当我调试时,我在更新期间运行以下几行,它们都返回 true,我认为这是一个矛盾。
@request.begin_date == @request.begin_date_was # Returns true
@request.begin_date_changed? # Returns true
我想知道它是否与更改初始值设定项中的默认日期格式(更改为“%m/%d/%Y”)或可能与时区有关。
我很困惑,所以任何帮助将不胜感激。
In my Rails 3 application, I only want to write an entry in a certain log if changes are actually made to a model. So if a user doesn't change any of the fields and clicks 'Submit', then there should not be a log entry.
But it seems that no matter what, Rails always seems to think that the DateTime attributes of the model have been changed.
When I debug, I run the following lines during my update and they both return true, which I would think would be a contradiction.
@request.begin_date == @request.begin_date_was # Returns true
@request.begin_date_changed? # Returns true
I'm wondering if it has something to do with changing the default date format in an initializer (to '%m/%d/%Y') or possibly something with time zones.
I'm stumped, so any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 en.yml 区域设置文件中更改默认日期和时间格式,如下所示:(这是我的项目之一中的法语格式示例)
或者您可以简单地将日期时间实例转换为:
@request.begin_date.strftime("%m/%d/%Y") == @request.begin_date_was.strftime("%m/%d/%Y")
甚至:
l(@request.begin_date, :format => your_format_in_locale_file) == l(@request.begin_date_was, :format => your_format_in_locale_file)
希望对您有帮助
You can change default date and time format in your en.yml locale file like this: (this is example for french format in one of my projects)
Or you can simply convert your datetime instances to:
@request.begin_date.strftime("%m/%d/%Y") == @request.begin_date_was.strftime("%m/%d/%Y")
or even:
l(@request.begin_date, :format => your_format_in_locale_file) == l(@request.begin_date_was, :format => your_format_in_locale_file)
Hope it will help you
我意识到,当您询问时,您可能使用的是不同的 Rails 版本,但我自己只是在 Rails 3.2.5 中偶然发现了这一点。显然它也是 3.2.5 中的回归: https://github.com/rails/rails/问题/6591
I realize when you asked, you were probably using a different Rails version, but I just stumbled upon this myself with Rails 3.2.5. Apparently its a regression in 3.2.5, too: https://github.com/rails/rails/issues/6591
我通过谷歌搜索来到这里寻找类似的问题。它与 Rails 版本无关,但经过一些调试后我发现我分配了一个带有毫秒的
Time
对象。当调用changed
时,我得到了看似相同的对象数组,因为它已转换为DateTime
的。不确定这是否可以被视为 Rails 中的错误,但如果您最终遇到同样的问题,请检查您是否分配了带有毫秒的日期时间。I arrived here from a google search for a similar problem. It wasn't related to the Rails version, but I found out after some debugging that I was assigning a
Time
object with milliseconds. When callingchanged
, I got back and array of seemingly identical objects, since it got converted toDateTime
's. Not sure if this can be considered a bug in Rails or not, but in case you end up with the same problem, check that you aren't assigning datetime's with milliseconds in them.