Class: ActiveRecord::UnionRelation::Subquery
- Inherits:
-
Object
- Object
- ActiveRecord::UnionRelation::Subquery
- Defined in:
- lib/active_record/union_relation.rb
Overview
This represents a combination of an ActiveRecord::Relation and a set of columns that it will pull.
Constant Summary collapse
- NULL =
Sometimes you need some columns in some subqeries that you don’t need in others. In order to accomplish that and still maintain the matching number of columns, you can put a null in space of a column instead.
Arel.sql('NULL')
Instance Attribute Summary collapse
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Instance Method Summary collapse
-
#initialize(relation, sources) ⇒ Subquery
constructor
A new instance of Subquery.
- #to_arel(columns, discriminator) ⇒ Object
- #to_mapping(columns) ⇒ Object
Constructor Details
#initialize(relation, sources) ⇒ Subquery
Returns a new instance of Subquery.
38 39 40 41 42 |
# File 'lib/active_record/union_relation.rb', line 38 def initialize(relation, sources) @relation = relation @model_name = relation.model.name @sources = sources.map { |source| source ? source.to_s : NULL } end |
Instance Attribute Details
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
36 37 38 |
# File 'lib/active_record/union_relation.rb', line 36 def model_name @model_name end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
36 37 38 |
# File 'lib/active_record/union_relation.rb', line 36 def relation @relation end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
36 37 38 |
# File 'lib/active_record/union_relation.rb', line 36 def sources @sources end |
Instance Method Details
#to_arel(columns, discriminator) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/active_record/union_relation.rb', line 44 def to_arel(columns, discriminator) relation .select( Arel.sql("'#{model_name}'").as(quote_column_name(discriminator)), *sources.zip(columns).map do |(source, column)| Arel.sql(source.to_s).as(quote_column_name(column)) end ) .arel end |
#to_mapping(columns) ⇒ Object
55 56 57 |
# File 'lib/active_record/union_relation.rb', line 55 def to_mapping(columns) [model_name, columns.zip(sources).to_h] end |