Ruby on Rails:借助 << 修改模型属性的虚拟方法无法保存该属性

发布于 2024-09-06 16:56:50 字数 1111 浏览 5 评论 0 原文

有带有属性注释(文本类型)的模型批准

def Ratification < ActiveRecord::Base
  attr_accessor :add_comment
  def add_comment=(text)
    self.comment ||= ""
    self.comment << "\r\n" + text
  end
end

,如果我使用add_comment=,那么在保存对象之前就可以了。保存后评论更改被删除。

>> r = Ratification.last
  Ratification Load (0.6ms)   SELECT * FROM `ratifications` ORDER BY ratifications.id DESC LIMIT 1
=> #<Ratification id: 8, user_id: 686, comment: "dasads", created_at: "2010-06-25 13:16:24", updated_at: "2010-06-25 13:38:36">
>> r.comment
=> "dasads"
>> r.add_comment="text"
=> "text"
>> r.comment
=> "dasads\r\ntext"
>> r.save
  SQL (0.7ms)   BEGIN
  SQL (0.2ms)   COMMIT
=> true
>> r.reload
  Ratification Load (1.6ms)   SELECT * FROM `ratifications` WHERE (`ratifications`.`id` = 8) 
=> #<Ratification id: 8, user_id: 686, comment: "dasads", created_at: "2010-06-25 13:16:24", updated_at: "2010-06-25 13:38:36">
>> r.comment
=> "dasads"

为什么?!

轨道2.3.8 红宝石1.8

There is model Ratification with attribute comment (of type text)

def Ratification < ActiveRecord::Base
  attr_accessor :add_comment
  def add_comment=(text)
    self.comment ||= ""
    self.comment << "\r\n" + text
  end
end

And if I use add_comment= it is ok before I save the object. After save comment changes was dropped.

>> r = Ratification.last
  Ratification Load (0.6ms)   SELECT * FROM `ratifications` ORDER BY ratifications.id DESC LIMIT 1
=> #<Ratification id: 8, user_id: 686, comment: "dasads", created_at: "2010-06-25 13:16:24", updated_at: "2010-06-25 13:38:36">
>> r.comment
=> "dasads"
>> r.add_comment="text"
=> "text"
>> r.comment
=> "dasads\r\ntext"
>> r.save
  SQL (0.7ms)   BEGIN
  SQL (0.2ms)   COMMIT
=> true
>> r.reload
  Ratification Load (1.6ms)   SELECT * FROM `ratifications` WHERE (`ratifications`.`id` = 8) 
=> #<Ratification id: 8, user_id: 686, comment: "dasads", created_at: "2010-06-25 13:16:24", updated_at: "2010-06-25 13:38:36">
>> r.comment
=> "dasads"

Why?!

Rails 2.3.8
Ruby 1.8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

断爱 2024-09-13 16:56:51

Hrrrm...这很奇怪,当我尝试执行以下操作时,我在 Rails 应用程序中看到类似的行为:

@s.name << "test"

然后重新加载...原始名称正在重置!

但是,如果我执行 @s.name += "test"

那么即使在重新加载后,新名称也会被保存。

我不知道为什么<<就是这样的,但我通常在所有情况下都默认为 += ,所以我以前从未注意到它。更改为 += 对您有帮助吗?

编辑:看看API,也许是因为<<修改原始字符串,而 + 或 += 生成一个包含旧字符串的新字符串?也许rails以某种方式只保存它标记为新的东西(而不是修改过的?)

Hrrrm...that IS weird, I'm seeing similar behavior from my rails app when I try to do:

@s.name << "test"

and then reload...the original name is getting reset!

HOWEVER, if I do @s.name += "test"

then even after reloading, the new name is saved.

I'm not sure why << is behaving like that, but I usually default to += in all cases, so I've never noticed it before. Does changing to += help you?

Edit: Looking at the API, maybe it's because << modifies the original string, whereas + or += makes a NEW string, that contains the old one? Maybe rails somehow only saves things that it has marked as new (rather than modified?)

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