how to NOT search active record in database - ruby-on-rails

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?

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

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.

Your question(s)
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
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?

Related

Using the gem paper_trail for tracking the latest news-activity on my website

I'm using the gem paper_trail for keeping track of versions. I wonder, is there any way to create some kind of activity news out of the models it's been included into? For example, I've added it into the models Commentary, User and Article. I want to be to get a list of the latest changes of these models and create something like news of activities:
user A has created a new Category
user B has edited an Article
admin had added a new Article
etc
Is it possible? And how? Seems like yes, but how exactly? Note that, I don't want to retrieve that kind of information from all the models I've included the gem into, but only from the ones I want to.
Since you want it by the user, you should integrate https://github.com/ankit1910/paper_trail-globalid to link the whodunnit part.
PaperTrail::Version.where_object(attr1: val1, attr2: val2)
NOTE: paper_trail gem is for auditing.
Do not forget to check your indexes on your database since relational databases slow as your no. of indexes increase.
Based on my experience with [spree][1] framework and a similar gem, versions table was the largest we had and it grew quiet fast. You may face similar challenges in maintaining this table and adding indexes may cause it to slow down.
My suggestion would be to use a Redis like store to maintain a list of 10 or so recent updates per user which the user may be notified of instead of querying MySQL (essentially caching)

Why does rails generate add timestamps to models by default?

I would expect that most models do not require information about when they were created and updated as a necessary feature of the model. Indeed, the getting started guide doesn't use or mention them. So what is the reasoning behind creating created_at/updated_at fields for models by default?
You may disable them from being created in your migrations if you would like. But to answer your question, you may be surprised how many models tend to have some sort of need to use data from the time stamps in some way, whether thats setting the default scope of that table, using the data to expire certain things, etc... They are included by default because they are useful and convenient to a lot of people, but if you are positive you don't need those feilds, feel free to get rid of them.
A timestamp is added to make it easier for user. A timestamp can be used to understand when what model was created and also to distinguish between different databases

Track a user who has updated data in Ruby on Rails

I am new to Ruby on Rails. The rails application that I have developed has several models including one for user that stores user name, passwords and other user related information.
Now the problem is that a few columns of a table corresponding to a model has modified erratically. Now I want to know if Ralis has any feature so that I can know the user who has done this or this is because of some other reasons.
You can try installing Userstamp and maybe Paper Trail to track changes to records. If you've implemented the User model yourself (as opposed to a framework like devise), you'll need to read the docs carefully to see what properties are expected of your User models to get the full benefit.
Using devise
It adds other columns(yours) in migrate, before generate views
https://github.com/collectiveidea/audited might provide the auditing you require.

Keeping track of who did what in Rails?

What's the best practice way for keeping track of who did what in a mid-sized Rails app? Intercepting all database read/writes and storing that in another table?
you can pretty simply adapt acts_as_versioned to also record information about which user performed the operation. I'd suggest looking into that plugin as versioning is rarely a bad idea.
You can use Observers on callbacks like create/update/delete for several models and save data to another table/model but if you want to have a wiki-like site - acs_as_versioned is better option.
If you'd like to "roll your own" solution you can implement database triggers on insert/update/delete that update a separate table. Otherwise there are several commercially supported data audit applications that can be purchased and configured to track and report on these activites for you at the database level.
Rather than reimplementing, you should try some plugins like acts_as_audited
acts_as_audited is an ActiveRecord extension that logs all changes to your models in an audits table.
or PaperTrail.
PaperTrail lets you track changes to your models' data. It's good for auditing or versioning. You can see how a model looked at any stage in its lifecycle, revert it to any version, and even undelete it after it's been destroyed.
Whichever suits your needs.

What's the best way to store the ActiveRecord Models with Versions and their Associations with Versions?

If all I have is one model (for example Wiki) and want to save it along with its versions, I could use acts_as_versioned plugin which stores the wikis in "wikis" table and its versions in "wikis_versions" table. This is plain an simple even if I want to moderate the latest version before showing it to the public using a field as status with "pending review/ published".
What's the best way to handle Wiki with associations (for example attachments, assets,..) which also have versions? And how would you moderate it? Do you create a new version to wiki even though only its association is changed just to keep the flow going, if so what about other associations?
What's the best way to handle it with little db overhead?
Thanks in advance.
I have used both acts_as_versioned and acts_as_audited.
I prefer the latter because it uses a single table. Using acts_as_versioned we've had issues with changes to versioned tables requiring extra migrations => this adds extra complexity to our build and deployment process.
Richard Livsey has a nice plugin for this that works with acts_as_versioned.
http://github.com/rlivsey/acts_as_versioned_association/tree/master

Resources