-
Notifications
You must be signed in to change notification settings - Fork 9
Bulk Operations
When dealing with truly large amounts of data, you may want to skip ActiveRecord instantiation — its overhead is usually minimal for most Web access patterns, but can be very significant when loading many thousands of records.
flex_columns
lets you create and save flex-column objects without requiring an instance of the enclosing ActiveRecord model to be present:
class User < ActiveRecord::Base
flex_column :details do
field :max_emails_per_week
field :background_color
end
end
raw_data = User.connection.select_all("SELECT id, details FROM users")
json_blobs = raw_data.map { |r| r['details'] }
flex_objects = User.create_flex_objects_from(:details, json_blobs)
flex_objects.each do |flex_object|
puts "max_emails_per_week: #{flex_object.max_emails_per_week}"
flex_object.max_emails_per_week += 1
end
new_json_blobs = flex_objects.map(&:to_stored_data)
The method #create_flex_objects_from
accepts the name of the flex column for which you want to create objects, and an array of Strings that should be valid data for that column; it returns an array of flex objects for that column, in the same order. (#create_flex_object_from
is identical, but takes a single String, rather than an array.) The strings can be JSON strings, or, if the column is binary, flex_columns
binary storage, compressed or uncompressed for that column.
The returned flex objects have a #to_stored_data
method, which will return the proper format for storage in the database. They also have a #to_json
method, which will always return pure, uncompressed JSON without a header, if you want that instead.
Flex objects do not deserialize their JSON until required, so they maintain as high performance as possible — if you don't read or write any of their fields, you will not pay this performance penalty. (The #touch!
method that each flex-column object has will force it to deserialize internally, if you just want to validate the object or delete unknown fields from it.)