background preloader

Data Management

Facebook Twitter

Auditing via paper_trail (change history) · gregbell/active_admin Wiki. Paper_trail is a gem used for auditing and versioning of your models.

Auditing via paper_trail (change history) · gregbell/active_admin Wiki

We can easily use it with active_admin to show a list of recently modified items in the admin screen. Example: Integration instructions Install PaperTrail as a gem via your Gemfile: gem 'paper_trail'Generate a migration which will add a versions table to your database: rails generate paper_trail:installRun the migration: rake db:migrateAdd user_for_paper_trail to controller so paper_trail knows which user updated the item. (optional) # app/controllers/application_controller.rbclass ApplicationController < ActionController::Base # ... protected def user_for_paper_trail admin_user_signed_in? Add has_paper_trail to the models you want to track: # app/models/post.rbclass Post < ActiveRecord::Base has_paper_trail # ...end Display the versions table on the dashboard Linking to model To link the item column to your model in active_admin, I added the following method to my model: Then in app/admin/dashboard.rb: Mysql - Rails 3 and saving decimal values from a form.

Transactions In Rails - Mark Daggett's Blog. Recently I was tasked to write tests for the transactions of an existing application.

Transactions In Rails - Mark Daggett's Blog

This gave me the opportunity to learn more about the codebase, while also improving the test coverage. Generally, most of the transaction code looked fine. However, there were some instances where transactions were used incorrectly or inefficiently. I assumed this is just because of a misunderstanding of how transactions work within Rails, and so I thought iʼd take some time and give an overview of the common errors I found and some best practices for using transactions in Rails. Let me also state at the beginning that most of these examples are not my own, they come directly from the Rails source, which give example usages of applying transactions RTFM FTW. Reasons for transactions We use transactions as a protective wrapper around SQL statements to ensure changes to the database only occur when all actions succeed together.

Transaction Rollback Triggers Note: The bang modifier (!) Transaction Gotchas. Rubygems - How do I read the content of an Excel spreadsheet using Ruby. Activerecord-import · zdennis/activerecord-import Wiki. Activerecord-import activerecord-import is a library for bulk inserting data using ActiveRecord.

activerecord-import · zdennis/activerecord-import Wiki

Note: activerecord-import requires Rails 3.×. It does not work with Rails 2.×. See the docs for 3.x Why activerecord-import? Because plain-vanilla, out-of-the-box ActiveRecord doesn’t provide support for inserting large amounts of data efficiently. 10.times do |i| Book.create! This may work fine if all you have is 10 records, but if you have hundreds, thousands, or millions of records it can turn into a nightmare. An Introductory Example To run examples on this page you’ll need to first require activerecord-import, for that please take a brief look at Requiring. Here’s an example with equivalent behaviour using the #import method: books = [] 10.times do |i| books << Book.new(:name => "book #{i}") end Book.import books This call to import does whatever is most efficient for the underlying database adapter.

Features. Efficient Updates & Data Import in Rails. By Ilya Grigorik on July 11, 2007 /* Typical ActiveRecord insert * number of records = slow */mysql> insert into widgets (title, price) values ('gizmo',5);Query OK, 1 row affected (0.01 sec) /* Multi-record insert with ar-extensions * 1 insert = fast */mysql> insert into widgets (title,price) values ('gizmo',5),('super-gizmo',10),('hammer',6);Query OK, 3 rows affected (0.01 sec)Records: 3 Duplicates: 0 Warnings: 0 ActiveRecord is a fantastic database abstraction layer and is rightfully one of the core pillars of Rails, but that doesn't mean we can't improve it.

Efficient Updates & Data Import in Rails

In fact, most Rails developers become painfully aware of this fact whenever they try to import or update a large number of records. The process of instantiating each object, validating it, and doing an individual save in a transactional database can be rather painful. SQL insert syntax - MySQL If you look at the MySQL documentation, you'll notice that INSERT is not limited to a single set of values.