When last a table column was modified - ruby-on-rails

Is there a way to know when a user updates a table column? For example, at what time a user changes their last name?
Im not interested when last a table was updated; only the column. Is it possible using Rails 5 and PostgreSQL?

If you are including timestamps in your models (.created_at and .updated_at) then .updated_at will tell you the last time that the record (i.e. the database row) was updated.
But that will not tell you which attribute of the record was changed (i.e. which database column). Nor will it tell you which user changed it, or if it was changed automatically by something in your system, etc.

You would need the schema to do that. You can a new table called as user_logs and implement a trigger which stores old_record and new_record. This will help you to get the desired log for change.

Related

Flutter - How to update the table structure in Sqflite?

My app is in production and I want to manage user data when user updates the app without loss of their data, how can I achieve this with sqflite. Explicitly I want to add a column and delete another.
You can probably add a column using raw sql, but sqlite (and thus sqflite) doesn't support dropping a column. For that you would need to do the following:
increase the database version number
in onUpgrade copy the old database columns to a temporary table
delete the original table
create a new table using the original table name but with the right schema
copy the data from the temp table
delete the temp table
Sorry, this isn't a full answer, but it is the direction I would go if I were in your situation.
I have the same problem and found this article which seems to be a good solution.

capture/store when fields in database records have been changed/edited

I have a multi user database system which stores records with various fields e.g. text, date time etc.
Does anyone know of a way to capture when fields of a record have been changed/modified by a user. A bit like a audit history which displays all the events which have happened against the record.
I connect to database via tdatasource and TADQuery (fireDAC).
Thanks,
I have seen a solution where every important table has a number of triggers that fire when inserting, updating, deleting records. Those triggers save the old and the new state of the record to a corresponding "history" table.

Creating a grouped text log of changed model attributes

I've recently started looking into Ruby on Rails, and I've set up a basic system to scan an parse and XML datasource, storing the elements in a MySQL database.
I'm intending to run the script as a rake task at set intervals, so want to track additions and updates, outputting the new, or changed, values to a text file.
I initially looked at using the before_save in order to write self.changes to a file, however the complexity arises as I'm retrieving data from two different pages and want to group the log output, e.g note each pricing row is a different record in the same table, ignore the variable names these are examples.
Item GUID
- Price US: #{old price} to #{new price}
- Price UK: #{old price} to #{new price}
The solution I'm currently looking to implement is appending a logged column to the table, if the data changes I can set this to changed, or new if the record has been added, and use this in a query to find records in which logged is not NULL, and group them by GUID. However as this will execute after the object has been saved I lose knowledge of the past values.
Is there a different approach I could take to achieve something like this?
Yes, there is a better way to do this. Take a look at these options you've got:
audited gem: https://github.com/collectiveidea/audited
paper_trail gem: https://github.com/airblade/paper_trail
espinita gem: https://github.com/continuum/espinita

Get the time/date a record was created

In Rails, is there an automatically instantiated field that has this information, or would I have to create a separate column in the model to keep track of it?
If you have columns called created_at and updated_at, they will update automatically. You can abbreviate that in your migration with t.timestamps, which now shows up by default (or did last time I created a model). If you're talking about an object stored in the database, there's no information about it other than what's in the table.
On a side note, if you're talking about an instance of an ActiveRecord it's best to say "record" instead of "object." The latter refers to any object that you instantiate, be it a record that you retrieve or just a simple Foo.new.
Rails automatically stores when each record is created in created_at it also stores when the record was last updated in updated_at

Rails - How to store each update of an item

I'm not sure how to do this in rails (maybe its a common topic but I'm not even sure if the title is correct)
I have a product table with this fields
Name
Quantity
Price
Size
and the columns that rails provide (id, created_at, updated_at)
This table is going to be updated periodically (lets say each day or so) but I want to save the QUANTITY that is being ADDED, and the date/time of the actualization (UPDATE).
I'm not sure if it's a design problem or something else.
Is there a way rails can handle this?
Thanks in advance
Javier QQ.
Given what you've said in your comments, why don't you just make a new table, say a "Stock" table. Each stock has two fields (in addition to the default created_at and updated_at): quantity and item_id.
Whenever you want to update an item with a new quantity, in the update method (or stock method, whatever it is) you do:
Stock.create(:item_id => #item.id, :quantity => params[:quantity])
This also ensures that you know when stock was added, because Rails will automatically keep track of when this Stock was made.
I'm not sure if this is exactly what you're looking for... but you can try the papertrail Gem. It stores each update of your model and you can easily step backwards or forwards in time through them to inspect your model and what fields changed, so it sounds like it'd be pretty ideal for what you have in mind.

Resources