How can I copy records from one table to another?

44 views
Skip to first unread message

ZeroModulus

unread,
Aug 13, 2012, 10:42:10 PM8/13/12
to rubyonra...@googlegroups.com
For every table in my app there is a corresponding deleted_.* table. I would like to be able to copy a record to its corresponding deleted_.* table. How might I achieve this?

If I have to have models for a deleted record, could I use some metaprogramming magic to create these models? How would I do that if so?

Agoofin

unread,
Aug 13, 2012, 11:20:12 PM8/13/12
to rubyonra...@googlegroups.com
How many records are you talking about?

For a few records, once in a while, you could do it in Ruby - Google using ActiveRecord outside of Rails for some more info.

Why do you have deleted tables? You might consider adding a field like "Active" and filter the non active records

ZeroModulus

unread,
Aug 13, 2012, 11:37:49 PM8/13/12
to rubyonra...@googlegroups.com
The number of records would vary widely.

I thought of doing deleted tables because I had already thought of having an attribute to mark a record as active, but figured I would have to write an exclusion clause every time.

Would there be a way to do this without having to write exclusion clauses for every query?

Michael Pavling

unread,
Aug 14, 2012, 3:15:01 AM8/14/12
to rubyonra...@googlegroups.com


On 14 Aug 2012 04:38, "ZeroModulus" <brlafr...@gmail.com> wrote:
>
> The number of records would vary widely.
>
> I thought of doing deleted tables because I had already thought of having an attribute to mark a record as active, but figured I would have to write an exclusion clause every time.
>
> Would there be a way to do this without having to write exclusion clauses for every query?
>

Have a look at the acts_as_paranoid gem.

Andrew Vit

unread,
Aug 14, 2012, 6:12:56 AM8/14/12
to rubyonra...@googlegroups.com
acts_as_paranoid simply adds a deleted_at flag, it doesn't move records to a separate table.

Something like acts_as_archive is a better equivalent.

Andrew Vit

Michael Pavling

unread,
Aug 14, 2012, 6:19:37 AM8/14/12
to rubyonra...@googlegroups.com
On 14 August 2012 11:12, Andrew Vit <and...@avit.ca> wrote:
> acts_as_paranoid simply adds a deleted_at flag, it doesn't move records to a
> separate table.

It does a *little more* than simply that...

> Something like acts_as_archive is a better equivalent.

The requirement to move them to another table was because the OP
didn't know how to "flag" records in the same table as deleted and
have them auto-magically ignored by finders, etc.

If the requirement is to archive them (which *isn't* the same as
softly deleting), along with the hassle of managing two sets of
tables... then acts_as_archived could be ideal.
Reply all
Reply to author
Forward
0 new messages