Module: CohortAnalysis::ActiveRecordRelationInstanceMethods

Defined in:
lib/cohort_analysis/active_record_relation_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#cohort(characteristics, options = {}) ⇒ Arel::SelectManager

Note:

This doesn’t return a ActiveRecord::Relation, so you can’t just call count.

Returns A select manager without any projections.

Examples:

Count a Flight cohort

cohort = Flight.cohort(:origin => 'MSN', :dest => 'ORD')
cohort.count #=> BAD! just plain Arel::SelectManager doesn't provide #count, that's an ActiveRecord::Relation thing
Flight.connection.select_value(cohort.project('COUNT(*)').to_sql) #=> what you wanted

Returns:

  • (Arel::SelectManager)

    A select manager without any projections.



11
12
13
14
15
16
# File 'lib/cohort_analysis/active_record_relation_instance_methods.rb', line 11

def cohort(characteristics, options = {})
  select_manager = arel.clone
  select_manager.projections = []
  select_manager.where Strategy.create(select_manager, characteristics, options)
  select_manager
end

#cohort_relation(characteristics, options = {}) ⇒ ActiveRecord::Relation

Note:

Won’t work properly unless it’s the last constraint in your chain.

Examples:

Making sure it’s the last thing you call

Flight.cohort_relation(:origin => 'MSN', :dest => 'ORD').where(:year => 2009) #=> BAD! the cohort calculation CANNOT see :year => 2009
Flight.where(:year => 2009).cohort_relation(:origin => 'MSN', :dest => 'ORD') #=> OK!

Returns:

  • (ActiveRecord::Relation)


25
26
27
# File 'lib/cohort_analysis/active_record_relation_instance_methods.rb', line 25

def cohort_relation(characteristics, options = {})
  where Strategy.create(arel, characteristics, options)
end