【问题标题】:How to use PaperTrail versioning with has_many :through to Reify associations in rails 4如何使用带有 has_many 的 PaperTrail 版本控制:通过在 Rails 4 中实现关联
【发布时间】:2015-01-09 18:19:53
【问题描述】:

尝试使用 PaperTrail 进行版本控制 我的联想没有恢复。我一定做错了什么。 这是与我的类似设置。我正在使用 PaperTrail 的最新主分支。

class Ball < ActiveRecord::Base
  has_many :ball_colors
  has_many :colors, through: :ball_colors
  has_paper_trail
end

class BallColor < ActiveRecord::Base
  belongs_to :ball 
  belongs_to :color
  has_paper_trail
end

class Color < ActiveRecord::Base
  has_paper_trail
  has_many :ball_colors
  has_many :balls, through: :ball_colors
end

这就是我正在做的。

ball = Ball.create()
ball.name = 'Before I add color'
ball.save

ball.colors << Color.create(name: 'blue')
ball.save #although this is unnecessary i think 

ball.name = 'After adding color'
ball.save

b = ball.versions.last.reify(:has_many => true)
b.save

b.reload
b.name  #=> 'Before I add color'
b.colors #=> [Blue] 

b.colors 应该是空的。 此外,当我向球添加颜色时,ball.versions 不包括更改。只有“添加颜色之前”的创建,更新,“添加颜色之后”的更新。 有人可以告诉我我做错了什么或指出一个例子吗? 我已阅读文档的关联部分,但无济于事。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 associations has-many-through paper-trail-gem


    【解决方案1】:

    如果我对文档的理解正确,则意味着书面记录能够恢复关联。

    请参阅项目github 页面关联章节。

    【讨论】:

      【解决方案2】:

      应该是ball.versions.last.reify(:has_many =&gt; true).save!reify 只是给你看我认为的以前的版本。

      【讨论】:

      • 我不能评论其他人的东西,但如果有人知道如何用validates :models, length: { minimum: 1 } 来具体化 has_many,那就太好了。记录在没有验证的情况下与关联正确恢复,但通过验证,它不会保存对验证失败的抱怨。
      猜你喜欢
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2013-05-06
      • 2014-12-16
      相关资源
      最近更新 更多