| AREXT_RAILS_COLUMNS | = | { :create => { "created_on" => tproc , "created_at" => tproc }, :update => { "updated_on" => tproc , "updated_at" => tproc } |
| AREXT_RAILS_COLUMN_NAMES | = | AREXT_RAILS_COLUMNS[:create].keys + AREXT_RAILS_COLUMNS[:update].keys |
| sanitize_sql | -> | sanitize_sql_orig |
| select_column_sql | -> | into_column_sql |
Count across a union of two or more queries
including :conditions, :join, :group, :having, and :limit
In addition the following options are accepted
Note that distinct is implied so a record that matches more than one portion of the union is counted only once.
To specify global options that apply to the entire union, specify a hash as the first parameter with a key :union_options. Valid options include :group, :having, :order, and :limit
Count the number of people who live in Seattle and San Francisco
Contact.count_union(:phone_number_id,
{:conditions => ['zip_id = ?, 94010]'},
{:conditions => ['zip_id = ?', 98102]})
SQL> select count(*) from ((select phone_number_id from contacts ...) UNION (select phone_number_id from contacts ...)) as counter_tbl;
Creates a temporary table given the passed in options hash. The temporary table is created based off from another table the current model class. This method returns the constant for the new new model. This can also be used with block form (see below).
class Project < ActiveRecord::Base ; end Project.create_temporary_table
This creates a temporary table named ‘temp_projects’ and creates a constant name TempProject. The table structure is copied from the projects table.
Project.create_temporary_table :table_name=>'my_projects', :model=>'MyProject'
This creates a temporary table named ‘my_projects’ and creates a constant named MyProject. The table structure is copied from the projects table.
ActiveRecord::Base.create_temporary_table :like=>Project
This is the same as calling Project.create_temporary_table.
Project.create_temporary_table do |t|
# ...
end
Using the block form will automatically drop the temporary table when the block exits. t which is passed into the block is the temporary table class. In the above example t equals TempProject. The block form can be used with all of the available options.
Delete all specified records with options
Multiple sql deletions are executed in order to avoid database contention This has no affect if used inside a transaction
Delete up to 65 red tags
Tag.delete_all ['name like ?', '%red%'], :limit => 65
Delete up to 65 red tags in batches of 20. This will execute up to 4 delete statements: 3 batches of 20 and the final batch of 5.
Tag.delete_all ['name like ?', '%red%'], :limit => 65, :batch => 20
Utility function to delete all but one of the duplicate records matching the fields specified. This method will make the records unique for the specified fields.
to c1 and c2 respectively
Make all the phone numbers of contacts unique by deleting the duplicates with the highest ids
Contacts.delete_duplicates(:fields=>['phone_number_id'])
Delete all tags that are the same preserving the ones with the highest id
Tag.delete_duplicates :fields => [:name], :winner_clause => "c1.id < c2.id"
Remove duplicate invitations (those that from the same person and to the same recipient) preseving the first ones inserted
Invitation.delete_duplicates :fields=>[:event_id, :from_id, :recipient_id]
Find a union of two or more queries
Each argument is a hash map of options sent to :find :all including :conditions, :join, :group, :having, and :limit
In addition the following options are accepted
Find the union of a San Fran zipcode with a Seattle zipcode
union_args1 = {:conditions => ['zip_id = ?', 94010], :select => :phone_number_id}
union_args2 = {:conditions => ['zip_id = ?', 98102], :select => :phone_number_id}
Contact.find_union(union_args1, union_args2, ...)
SQL> (SELECT phone_number_id FROM contacts WHERE zip_id = 94010) UNION
(SELECT phone_number_id FROM contacts WHERE zip_id = 98102) UNION ...
To specify global options that apply to the entire union, specify a hash as the first parameter with a key :union_options. Valid options include :group, :having, :order, and :limit
Example:
Contact.find_union(:union_options => {:limit => 10, :order => 'created_on'},
union_args1, union_args2, ...)
SQL> ((select phone_number_id from contacts ...) UNION (select phone_number_id from contacts ...)) order by created_on limit 10
Adds fulltext searching capabilities to the current model for the given fulltext key and option hash.
class Book < ActiveRecord::Base
fulltext :title, :fields=>%W( title publisher author_name )
end
# To use the fulltext index
Book.find :all, :conditions=>{ :match_title => 'Zach' }
Imports a collection of values to the database.
This is more efficient than using ActiveRecord::Base#create or ActiveRecord::Base#save multiple times. This method works well if you want to create more than one record at a time and do not care about having ActiveRecord objects returned for each record inserted.
This can be used with or without validations. It does not utilize the ActiveRecord::Callbacks during creation/modification while performing the import.
Model.import array_of_models Model.import column_names, array_of_values Model.import column_names, array_of_values, options
With this form you can call import passing in an array of model objects that you want updated.
The first parameter column_names is an array of symbols or strings which specify the columns that you want to update.
The second parameter, array_of_values, is an array of arrays. Each subarray is a single set of values for a new record. The order of values in each subarray should match up to the order of the column_names.
The first two parameters are the same as the above form. The third parameter, options, is a hash. This is optional. Please see below for what options are available.
class BlogPost < ActiveRecord::Base ; end
# Example using array of model objects
posts = [ BlogPost.new :author_name=>'Zach Dennis', :title=>'AREXT',
BlogPost.new :author_name=>'Zach Dennis', :title=>'AREXT2',
BlogPost.new :author_name=>'Zach Dennis', :title=>'AREXT3' ]
BlogPost.import posts
# Example using column_names and array_of_values
columns = [ :author_name, :title ]
values = [ [ 'zdennis', 'test post' ], [ 'jdoe', 'another test post' ] ]
BlogPost.import columns, values
# Example using column_names, array_of_value and options
columns = [ :author_name, :title ]
values = [ [ 'zdennis', 'test post' ], [ 'jdoe', 'another test post' ] ]
BlogPost.import( columns, values, :validate => false )
# Example synchronizing existing instances in memory
post = BlogPost.find_by_author_name( 'zdennis' )
puts post.author_name # => 'zdennis'
columns = [ :author_name, :title ]
values = [ [ 'yoda', 'test post' ] ]
BlogPost.import posts, :synchronize=>[ post ]
puts post.author_name # => 'yoda'
The :on_duplicate_key_update option can be either an Array or a Hash.
The :on_duplicate_key_update option can be an array of column names. The column names are the only fields that are updated if a duplicate record is found. Below is an example:
BlogPost.import columns, values, :on_duplicate_key_update=>[ :date_modified, :content, :author ]
The :on_duplicate_key_update option can be a hash of column name to model attribute name mappings. This gives you finer grained control over what fields are updated with what attributes on your model. Below is an example:
BlogPost.import columns, attributes, :on_duplicate_key_update=>{ :title => :title }
This returns an object which responds to failed_instances and num_inserts.
Imports the passed in column_names and array_of_attributes given the passed in options Hash with validations. Returns an object with the methods failed_instances and num_inserts. failed_instances is an array of instances that failed validations. num_inserts is the number of inserts it took to import the data. See ActiveRecord::Base.import for more information on column_names, array_of_attributes and options.
Imports the passed in column_names and array_of_attributes given the passed in options Hash. This will return the number of insert operations it took to create these records without validations or callbacks. See ActiveRecord::Base.import for more information on column_names, +array_of_attributes_ and options.
Insert records in bulk with a select statement
Any valid finder options (options for ActiveRecord::Base.find(:all) )such as :joins, :conditions, :include, etc including:
Create cart items for all books for shopping cart <tt>@cart+ setting the copies field to 1, the updated_at field to Time.now and the created_at field to the database function now()
CartItem.insert_select(:from => :book,
:select => ['books.id, ?, ?, ?, now()', @cart.to_param, 1, Time.now],
:into => [:book_id, :shopping_cart_id, :copies, :updated_at, :created_at]})
GENERATED SQL example (MySQL):
INSERT INTO `cart_items` ( `book_id`, `shopping_cart_id`, `copies`, `updated_at`, `created_at` ) SELECT books.id, '134', 1, '2009-03-02 18:28:25', now() FROM `books`
A similar example that
CartItem.insert_select(:from => Book,
:select => ['books.id, ?, ?, ?, now()', @cart.to_param, 1, Time.now],
:into => 'cart_items.book_id, shopping_cart_id, copies, updated_at, created_at',
:on_duplicate_key_update => [:updated_at])
GENERATED SQL example (MySQL):
INSERT INTO `cart_items` ( cart_items.book_id, shopping_cart_id, copies, updated_at, created_at )
SELECT books.id, '138', 1, '2009-03-02 18:32:34', now() FROM `books`
ON DUPLICATE KEY UPDATE `cart_items`.`updated_at`=VALUES(`updated_at`)
Similar example ignoring duplicates
CartItem.insert_select(:from => :book,
:select => ['books.id, ?, ?, ?, now()', @cart.to_param, 1, Time.now],
:into => [:book_id, :shopping_cart_id, :copies, :updated_at, :created_at],
:ignore => true)
Returns true if the current connection adapter supports full text searching, otherwise returns false.
Returns true if the current database connection adapter supports import functionality, otherwise returns false.
Returns true if the current database connection adapter supports on duplicate key update functionality, otherwise returns false.
Synchronizes the passed in ActiveRecord instances with data from the database. This is like calling reload on an individual ActiveRecord instance but it is intended for use on multiple instances.
This uses one query for all instance updates and then updates existing instances rather sending one query for each instance