Module: CompositePrimaryKeys::ActiveRecord::Associations::ClassMethods
- Defined in:
- lib/composite_primary_keys/associations.rb
Overview
Composite key versions of Association functions
Instance Method Summary collapse
- #construct_counter_sql_with_included_associations(options, join_dependency) ⇒ Object
- #construct_finder_sql_with_included_associations(options, join_dependency) ⇒ Object
- #quoted_table_columns(columns) ⇒ Object
- #table_columns(columns) ⇒ Object
Instance Method Details
#construct_counter_sql_with_included_associations(options, join_dependency) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/composite_primary_keys/associations.rb', line 12 def construct_counter_sql_with_included_associations(, join_dependency) scope = scope(:find) sql = "SELECT COUNT(DISTINCT #{quoted_table_columns(primary_key)})" # A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT. if !self.connection.supports_count_distinct? sql = "SELECT COUNT(*) FROM (SELECT DISTINCT #{quoted_table_columns(primary_key)}" end sql << " FROM #{table_name} " sql << join_dependency.join_associations.collect{|join| join.association_join }.join add_joins!(sql, , scope) add_conditions!(sql, [:conditions], scope) add_limited_ids_condition!(sql, , join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || [:limit]) add_limit!(sql, , scope) if using_limitable_reflections?(join_dependency.reflections) if !self.connection.supports_count_distinct? sql << ")" end return sanitize_sql(sql) end |
#construct_finder_sql_with_included_associations(options, join_dependency) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/composite_primary_keys/associations.rb', line 37 def construct_finder_sql_with_included_associations(, join_dependency) scope = scope(:find) sql = "SELECT #{column_aliases(join_dependency)} FROM #{(scope && scope[:from]) || [:from] || table_name} " sql << join_dependency.join_associations.collect{|join| join.association_join }.join add_joins!(sql, , scope) add_conditions!(sql, [:conditions], scope) add_limited_ids_condition!(sql, , join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && [:limit] sql << "ORDER BY #{[:order]} " if [:order] add_limit!(sql, , scope) if using_limitable_reflections?(join_dependency.reflections) return sanitize_sql(sql) end |
#quoted_table_columns(columns) ⇒ Object
57 58 59 |
# File 'lib/composite_primary_keys/associations.rb', line 57 def quoted_table_columns(columns) table_columns(columns).join(ID_SEP) end |
#table_columns(columns) ⇒ Object
53 54 55 |
# File 'lib/composite_primary_keys/associations.rb', line 53 def table_columns(columns) columns.collect {|column| "#{self.table_name}.#{column}"} end |