2

I have Follow model in my rails app and when user follow some content - record created, unfollow - record deleted.

I need track this changes in separate table (insert and delete only).

It will be used to build some reports with plain SQL - thats why table should have similar structure aka "shadow"

I don't want polute original model with additional logic - there is some solutions but they don't flexible enough:

https://github.com/rubiety/has_draft - I can't select specific operations

https://github.com/bglusman/ruby-shadow-tables - raw and mysql only

Does anybody know some good solution for such case?

Volodymyr
  • 1,136
  • 3
  • 11
  • 28

3 Answers3

0

You can create one more table shadow_follows and have a callback method after_create and before_destroy to copy the target record to new table.

pravs
  • 27
  • 4
  • Yeah I think about thats but it looks like functionality which can be delegated to gem – Volodymyr Feb 19 '13 at 13:17
  • https://rubygems.org/gems/audited gem is there but it wont create replica of the table; though you can specify which all tables can be audited. – pravs Feb 19 '13 at 13:21
0

I don't find any suitable gem for such case and decide implement it with rails observer. nothing special.. just simplest solution.

Volodymyr
  • 1,136
  • 3
  • 11
  • 28
0

This is fairly old, but in case someone else wants to do something like this, a friend and I did a script to set up shadow tables in rails with DB triggers... https://github.com/bglusman/ruby-shadow-tables

It's a bit crufty, and would definitely benefit from being abstracted into a gem, and perhaps a DB neutral solution.

bglusman
  • 43
  • 6