What would be the rails way of implementing version control in my record management application?
My system allows users to manage Records and i want to allow them to view historical versions of a Record. i know instead of updating a Record I will now create a new instance of the Record and related models every time a user "updates" a record(each Record has_many Categories and Advantages). how would i ensure that different versions of the Record are all linked together (i.e the new updated record created to be associated as the new version of record A, so when i click "show me a list of all versions of record A").
this is all theoretical thinking as i am yet to start coding, if i missed anything which i should also consider please let me know.
Thank You
create a new instance of the record every-time a user updates it, have a secondary ID as you mentioned to group all different versions of the same record together and then run a check in the controller (using some sort of hidden value) to see if you want to save over the record or create a new one.
You can then retrive the latest version of each record by finding the most recently updated/created record with a unique secondary_id.
A good starting point may be the vestal versions gem, that keeps an history of modified records
Here are 2 of my insights:
1st simple one :
You save a record with a higher id , when you read it you take the higher id. If you want to know the past do not filter on ids.
2nd (parts from SAP) :
Your record has 2 supplemental fields , that is startTime, stopTime. Thoses are the time the record start entering in action and stops being in action. Inserting a new record , you update the stopTime of the last one, put now as the startTime of the new one, and the end of the world for the stopTime of the new one
Related
Recently I had to create a couple of records in a non-rails app database table based on a previous record. It got me thinking of how would I do this in a rails app. I tried a couple of things in the Console, but nothing works.
I want to do something like this:
001> user = User.new(User.first)
I know this doesn't work but hopefully it will show you what I an thinking. User is a large table/model, and I only need to change a few fields. So, if I can set up a new record with the same values in User.first, I can then edit the fields I need to before .save-ing the record.
Thanks for any help.
I think what you want is:
user = User.first.dup
user.assign_attributes(email: "myemail#test.test")
user.save
The first line uses dup to create a copy of the object. The copy is not yet saved to the database. Replace dup with clone if you're using an old version of Rails (<3.1).
In the second line, assign_attributes alters the attributes of the object, still without saving it to the database. If you were working with an object already saved in the database, you could use update instead of assign_attributes to change the attributes of the object and save the changes in one go. That won't work here, because we haven't saved our duplicate user yet. More details on that here.
The third line finally saves the new object to the database. It saves time to just do this once, at the end.
I have a problem with active_record query, sorry, I'm new with rails.
I have a model Subscription, it has child SubscriptionVersion, where I keep all info that can be updated.
At API calls, for example get request, I merge data from subscription and current version (#subscription.subscription_versions.last) and give that merged hash for response, but I've faced with problem of sorting and filtering by values in that current_version.
When I try do that
#subscriptions.joins(:subscription_versions).order('subscription_versions.version') it takes all version and sort by min value, even if subscription has 5 versions and latest is 5th (current_version).
So ... can anyone give me advice how to generate query for that without adding new field in subscription model current_version_id?
If I understand correctly, you want only to return the last subscription_version?
Depending on how the versions are sorted, you could do this (Haven't tested this):
#subscriptions.joins(:subscription_versions).order('subscription_versions.version').first
or
#subscriptions.joins(:subscription_versions).order('subscription_versions.version').last
Im using paper_trail with rails 4 and i would like to be able to do this:
what i want to do is run a crud action but i dont want it to be shown instant on the site, what i want is, it to be saved in the version table and later run a action let say at night wich will update the site with the changed data from the versioning.
So lets say i have a product:
Name: Nike, Color: Blue
And during the day i add a new product:
Name:Nike, Color:Black
I dont want this new product to be shown right away, but i want it to be stored in papertrail and at a later time take the stored data and show it.
This goes for all the crud operations.
So if i would change the name of a product, the old one would still be shown until i run the stored data from papertrail and display it.
Hope iv managed to explaing my question, having some troubles wrapping my head around it.
Thank you for your time.
I would recommend a more lightweight solution to your problem:
You can store a state with your data, lets say "new" is the first state when a user submits the data. In your application you use a default_scope to make sure that only data with the state of "reviewed" or "published" or whatever are visible to users.
You can then do whatever magical operation you want to perform at night without having a lot of trouble copying data from A to B.
I have a model which I want to store a history of changes to, my plan is to rather than update an object create a new one and on a show only fetch the latest version.
This plan presents a number of difficulties firstly the id will be different after a update I indend to get around this by keeping a second ID column which will be the same for all updates of that instance.
to that end I have created a SQLite sequence for this second coloumn.
my question is how can I get values from this sequence in the model/controller as I will only want to get from it on first time the object is created, secondly how can I use this second ID column as the URL for the object so it is fixed throughout updates.
Many Thanks,
Check out the PaperTrail gem. It might do what you want and sidestep those issues completely.
https://github.com/airblade/paper_trail
in the application i am currently creating in ruby on rails. I am trying to do some tests in rails console where i have to destroy data in the database and the database is connected to a server. I am importing an XML and parsing it and putting it into a database with scaffolding.
Now what i need: Basically what i am attempting to do is to destroy the data and replace it with a new one every week..but the problem i am getting, the userid is gone up to 700+ and there are only 50 records :S cause it doesnt reset...
To delete all records i am currently using "whatever.destroy_all" does the trick
Any help?
Btw i am using SQLITE
The ID column created in the table usually is set as unique and to increment by 1 for each new record, which is why each time you destroy and add new data the ID keeps getting higher.
The fact that the ID # is getting larger and larger is not an issue at all.
If you really want to start back at zero, I would think you could drop the table and recreate it, but that seems like overkill for a trivial issue.
Regarding the connection to the other scaffold, how are you connecting the two and what do they both represent?
Ideally the data population for testing should be done through fixtures (or easy tools such as factorygirl etc..)
The main advantage of having a fix data set is you can run your tests in any environment. But as per your requirement you can do something like this,
When you populate the date through the active records pass the id parameter as well
Ex: User.new(:id => 1, :name => "sameera").create
By this way you can have constant id's But make sure you increment the id accordingly.