在 Rails 3 模型中,DateTime 属性实际上是“脏”属性。

发布于 2024-11-04 11:15:23 字数 400 浏览 1 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨巷深深 2024-11-11 11:15:23

您可以在 en.yml 区域设置文件中更改默认日期和时间格式,如下所示:(这是我的项目之一中的法语格式示例)

date:
 formats:
  default: "%d/%m/%Y"
  short: "%e %b"
  long: "%e %B %Y"
  long_ordinal: "%e %B %Y"
  only_day: "%e"
time:
 formats:
  default: "%d %B %Y %H:%M"
  time: "%H:%M"
  short: "%d %b %H:%M"
  long: "%A %d %B %Y %H:%M:%S %Z"
  long_ordinal: "%A %d %B %Y %H:%M:%S %Z"
  only_second: "%S"
 am: 'am'
 pm: 'pm'

或者您可以简单地将日期时间实例转换为:
@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)

date:
 formats:
  default: "%d/%m/%Y"
  short: "%e %b"
  long: "%e %B %Y"
  long_ordinal: "%e %B %Y"
  only_day: "%e"
time:
 formats:
  default: "%d %B %Y %H:%M"
  time: "%H:%M"
  short: "%d %b %H:%M"
  long: "%A %d %B %Y %H:%M:%S %Z"
  long_ordinal: "%A %d %B %Y %H:%M:%S %Z"
  only_second: "%S"
 am: 'am'
 pm: 'pm'

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

对风讲故事 2024-11-11 11:15:23

我意识到,当您询问时,您可能使用的是不同的 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

只是在用心讲痛 2024-11-11 11:15:23

我通过谷歌搜索来到这里寻找类似的问题。它与 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 calling changed, I got back and array of seemingly identical objects, since it got converted to DateTime'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文