如何跟踪模型历史的映射表在Ruby on Rails的? [英] How to keep track of model history with mapping table in Ruby on Rails?

查看:147
本文介绍了如何跟踪模型历史的映射表在Ruby on Rails的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

梦想

我想继续当用户改变他们的地址的记录。

这样,当一个订单时,它总是能够引用被用于下订单时的用户地址。

可能的模式

 用户(
  ID
  用户名
  电子邮件
  ...
)

user_addresses(
  ID
  标签
  line_1
  line_2
  市
  州
  拉链
  ...
)

user_addresses_map(
  用户帐号
  user_address_id
  开始时间
  时间结束
)

订单(
  ID
  用户帐号
  user_address_id
  order_status_id
  ...
  created_at
  的updated_at
)
 

在SQL中,这可能会是这样:[SQL]

 选择UA。*

从订单Ø

加入用户ü
  在u.id = o.user_id

加入user_addressses_map UAM
  在uam.user_id = u.id
  和uam.user_address_id = o.user_address_id

加入user_addresses UA
  在ua.id = uam.user_address_id
  和uam.start_time< o.created_at
  和(uam.end_time> = o.created_at或uam.end_time为null)
;
 

修改:解决方案

@KandadaBoggu发布了很好的解决方案。在 维斯塔版本插件 伟大的解决方案

下面的代码片段从<一个取href="http://github.com/laserlemon/vestal_versions">http://github.com/laserlemon/vestal_versions

最后,干ActiveRecord的版本!

  

acts_as_versioned 通过 technoweenie 是良好的开端,但它未能跟上2.1版本的ActiveRecord的介绍肮脏的对象。此外,每个版本模型需要它自己的版本表中复制最原始表的列。然后填充版本表与记录,往往重复原来的大部分记录的属性。总而言之,不是很干。

     

vestal_versions 只需要一个版本表(多态与其父模型相关的),并没有任何的变化对现有的表。但进了一步干燥机通过存储的只有的模特们的变化序列化哈希值。认为现代的版本控制系统。通过遍历变化的记录,该模型可以恢复到任意时间点。

     

而这正是ve​​stal_versions一样。 不仅可以将模型恢复为previous版本号,但也为日期或时间!

解决方案

使用维斯塔版本的插件以这样的:

参阅这个屏幕投的更多细节。

 类地址&LT;的ActiveRecord :: Base的
  belongs_to的:用户
  版本控制
结束


一流的订购与LT;的ActiveRecord :: Base的
   belongs_to的:用户

   DEF地址
     @address || =(user.address.revert_to(的updated_at)和user.address)
   结束
结束
 

dream

I'd like to keep record of when a user changes their address.

This way, when an order is placed, it will always be able to reference the user address that was used at the time of order placement.

possible schema

users (
  id
  username
  email
  ...
)

user_addresses (
  id
  label
  line_1
  line_2
  city
  state
  zip
  ...
)

user_addresses_map (
  user_id
  user_address_id
  start_time
  end_time
)

orders (
  id
  user_id
  user_address_id
  order_status_id
  ...
  created_at
  updated_at
)

in sql, this might look something like: [sql]

select ua.*

from  orders    o

join  users     u
  on  u.id = o.user_id

join  user_addressses_map   uam
  on  uam.user_id = u.id
  and uam.user_address_id = o.user_address_id

join  user_addresses        ua
  on  ua.id = uam.user_address_id
  and uam.start_time < o.created_at
  and (uam.end_time >= o.created_at or uam.end_time is null)
;

edit: The Solution

@KandadaBoggu posted a great solution. The Vestal Versions plugin is a great solution.

snippet below taken from http://github.com/laserlemon/vestal_versions

Finally, DRY ActiveRecord versioning!

acts_as_versioned by technoweenie was a great start, but it failed to keep up with ActiveRecord’s introduction of dirty objects in version 2.1. Additionally, each versioned model needs its own versions table that duplicates most of the original table’s columns. The versions table is then populated with records that often duplicate most of the original record’s attributes. All in all, not very DRY.

vestal_versions requires only one versions table (polymorphically associated with its parent models) and no changes whatsoever to existing tables. But it goes one step DRYer by storing a serialized hash of only the models’ changes. Think modern version control systems. By traversing the record of changes, the models can be reverted to any point in time.

And that’s just what vestal_versions does. Not only can a model be reverted to a previous version number but also to a date or time!

解决方案

Use the Vestal versions plugin for this:

Refer to this screen cast for more details.

class Address < ActiveRecord::Base
  belongs_to :user
  versioned
end


class Order < ActiveRecord::Base
   belongs_to :user

   def address
     @address ||= (user.address.revert_to(updated_at) and user.address)
   end
end

这篇关于如何跟踪模型历史的映射表在Ruby on Rails的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆