【问题标题】:Is this a bug in Vestal Versions or am I doing something wrong这是 Vestal 版本中的错误还是我做错了什么
【发布时间】:2009-11-03 08:03:14
【问题描述】:

我想我可能在 Vestal 版本 (http://github.com/laserlemon/vestal_versions) 中发现了一个错误——似乎revert_to 的行为取决于我过去对同一个对象所做的还原。这是一个例子:

>> a = Annotation.find(1384)
=> #<Annotation id: 1384, body: "Just hanging out -- \"playing possum\" -- at the stor...", last_updated_by_id: 3, created_by_id: 3, song_id: 30, deleted_at: nil, created_at: "2009-09-06 01:56:55", updated_at: "2009-10-27 22:02:35", referent: "in the spot playing possum\nDebating my destination,...", vote_score: 0>
>> a.revert_to(9)
=> 9
>> a.body
=> #<RDiscount:0x21cf7bc @filter_styles=true, @smart=true, @fold_lines=nil, @filter_html=nil, @text="Just hanging out -- \"playing possum\" -- at the store, lacing up the new Nikes, trying to decide where to go for dinner">


>> a = Annotation.find(1384)
=> #<Annotation id: 1384, body: "Just hanging out -- \"playing possum\" -- at the stor...", last_updated_by_id: 3, created_by_id: 3, song_id: 30, deleted_at: nil, created_at: "2009-09-06 01:56:55", updated_at: "2009-10-27 22:02:35", referent: "in the spot playing possum\nDebating my destination,...", vote_score: 0>
>> a.revert_to(8)
=> 8
>> a.body
=> #<RDiscount:0x21b5a10 @filter_styles=true, @smart=true, @fold_lines=nil, @filter_html=nil, @text="I.e. just hanging out -- \"playing possum\" -- in the living room, lacing up the new Nikes, trying to decide where to go for dinner">
>> a.revert_to(:last)
=> 11
>> a.revert_to(9)
=> 9
>> a.body
=> #<RDiscount:0x21b5a10 @filter_styles=true, @smart=true, @fold_lines=nil, @filter_html=nil, @text="I.e. just hanging out -- \"playing possum\" -- in the living room, lacing up the new Nikes, trying to decide where to go for dinner">

即,如果我 revert_to(9) 来自新加载的注释,则正文字段包含一个 RDiscount 对象,其文本以“Just hang out -- \"playing possum\" -- at the store”开头(这就是正文从第 9 版开始)

但是,如果我从新加载的注解恢复到 revert_to(8),请检查注解的正文,revert_to(:last)revert_to(9),注解的正文在版本 9 中将是错误的(它将匹配注释的正文版本 8)

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails vestal-versions


    【解决方案1】:

    这不是vestal_versions 中的错误,而是版本更改后rails 没有重新加载您的关联。 假设您的 Annotation 将 id 保存在您的 RDiscount 中,则会发生以下情况:

    1. 您获取Annotation“a”,RDiscount id 为 x。
    2. 您将“a”恢复到以前的版本,RDiscount id 更改为 y。
    3. 您调用a.body,导致rails 加载ID 为y 的RDiscount 对象。
    4. 您将“a”恢复为:lastRDiscount id 再次变为 x。
    5. 您再次调用a.body,但rails 已经加载了RDiscount 对象并将改为返回此对象。

    【讨论】:

    • 你可以这样调用你的关联:a.body(true)。这会强制 rails 从数据库中获取关联。可能还有其他使用 Vestal 版本的方法,但我现在想不出任何漂亮的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    相关资源
    最近更新 更多