Question

long time reader first time poster.

I recently started using ruby on rails so I am still very new to the environment (even though I have completed a few guides) so be gentle please.

What I want to do is create a sort of archive table of another table that the user can access at any time(via a different link on the website).

So for example, if I have the "users" table, I want to be able to archive old users but still give the option for someone to go and view them. Basically, it will sort of have to delete the user from the initial table, and save his/her info in to the archived_users table.

Thank you for your time.

Was it helpful?

Solution

I figured my comment was more of an answer, so posting it here and adding more info

In this situation you're better off adding some sort if "active" flag to the users table, which you can flip on or off as needed. That way you don't need to worry about dealing with yet another model class, and you can reuse all the same view and controller structures. In your views, you can then simply "hide" any inactive users (and maybe only show inactive folks if the logged in user is an admin...etc).

You also have the freedom to include other meta data such as "deactivated on" (time stamp) for example.

Long story short, if you're concerned about performance, with proper indexing (and partitioning if necessary), you shouldn't really need to create a separate archive table.

The only reason I can think of to do this is if you're dealing with billions upon billions of records, and/or growing by an insane amount (which is probably not your case).

OTHER TIPS

The best way to do this is probably to add a column called deleted on the original Users table. You can then filter out the old users in normal circumstances (preferably using a default scope) but allow them to be seen/queried when needed.

Papertrail might work for you.

It creates a "versions" table and logs create/update/destroy events for any class which includes has_paper_trail. For example:

class User < ActiveRecord::Base
  has_paper_trail
end

deleted_users = Papertrail::Version.where(item_type: User, event: "destroy")

deleted_users.last.reify.name # assuming the users table has a 'name' column
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top