nathanQQ
nathanQQ

Reputation: 35

how to NOT search active record in database

I am new to RoR. I met one question which is: some active records may not needed to be searched for in database(let's say they are expired). I don't want to delete it from database because it is associated with other models. Also since it is expired, I don't want it to be updated. Is there any way to handle this situation in rails?

Upvotes: 0

Views: 74

Answers (3)

seartles
seartles

Reputation: 36

Your question(s)

  1. Avoid querying a subset of your data: *For this one ought to utilize ActiveRecord's scopes. Specifically use a default scope. Ref here http://api.rubyonrails.org/classes/ActiveRecord/Scoping/Default/ClassMethods.html. Incidentally, this is the mechanism that the soft-delete gems I have come across utilize to hide soft-delete records
  2. How to prevent updating the records: Not sure how you mean. What about the records that you are not going to be using will be (automatically?) updated?

Upvotes: 0

user3118220
user3118220

Reputation: 1468

A Rails plugin to add soft delete.

This gem can be used to hide records instead of deleting them, making them recoverable later.

https://github.com/ActsAsParanoid/acts_as_paranoid

Upvotes: 1

Bjoernsen
Bjoernsen

Reputation: 2418

There are different ways. But I recommend you to use https://github.com/radar/paranoia

You can find a good screencast here: https://gorails.com/episodes/soft-delete-with-paranoia

It is really easy to add to your app. Just add the gem to your Gemfile and add acts_as_paranoid to your model.

Upvotes: 0

Related Questions