| Module | EagerFinderSql::ActiveRecord |
| In: |
lib/eager_finder_sql.rb
|
Allow finder_sql to be used for eager loading of associations. Extends ActiveRecord find syntax when other associations are eagerly loaded using :include to allow for a :finder_sql option that specifies custom SQL to use for the query. A separate +:column_mapping+ hash specifies the mapping of returned columns to objects and their attributes. Column Mapping looks like the following: {
:primary_key => 'column_name',
:columns => {'col1' => 'alias1', 'col2' => 'alias2', 'coln' => 'aliasn'}
:associations=> {
:reflection_name => {recursive mapping},
:reflection_name => ...
}
} For example, consider the mapping for the following table: CREATE TABLE authors (
id int(11) NOT NULL auto_increment, name varchar(255) default NULL,
) The model will have id and _name_attributes. The following SQL and column mapping would create the proper associations: :columns => {‘id’ => ‘author_id’, ‘name’ => ‘author_name’} :finder_sql => SELECT id AS author_id, name AS author_name FROM authors
Alternatively, :columns can be hash as follows: :columns => [[‘col1’, ‘alias1’], [‘col2’, ‘alias2’], …, [‘coln’, ‘aliasn’] — includes primary key