Class: ActiveRecord::UnionRelation
- Inherits:
-
Object
- Object
- ActiveRecord::UnionRelation
- Defined in:
- lib/active_record/union_relation.rb,
lib/active_record/union_relation/version.rb
Defined Under Namespace
Classes: Error, MismatchedColumnsError, NoConfiguredSubqueriesError, Subquery
Constant Summary collapse
- VERSION =
"0.3.1"
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#discriminator ⇒ Object
readonly
Returns the value of attribute discriminator.
-
#subqueries ⇒ Object
readonly
Returns the value of attribute subqueries.
Instance Method Summary collapse
-
#add(relation, *sources) ⇒ Object
Adds a subquery to the overall union.
-
#all ⇒ Object
Creates an ActiveRecord::Relation object that will pull all of the subqueries together.
-
#initialize(columns, discriminator) ⇒ UnionRelation
constructor
A new instance of UnionRelation.
Constructor Details
#initialize(columns, discriminator) ⇒ UnionRelation
Returns a new instance of UnionRelation.
122 123 124 125 126 |
# File 'lib/active_record/union_relation.rb', line 122 def initialize(columns, discriminator) @columns = columns.map(&:to_s) @discriminator = discriminator @subqueries = [] end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
120 121 122 |
# File 'lib/active_record/union_relation.rb', line 120 def columns @columns end |
#discriminator ⇒ Object (readonly)
Returns the value of attribute discriminator.
120 121 122 |
# File 'lib/active_record/union_relation.rb', line 120 def discriminator @discriminator end |
#subqueries ⇒ Object (readonly)
Returns the value of attribute subqueries.
120 121 122 |
# File 'lib/active_record/union_relation.rb', line 120 def subqueries @subqueries end |
Instance Method Details
#add(relation, *sources) ⇒ Object
Adds a subquery to the overall union.
129 130 131 132 133 134 135 |
# File 'lib/active_record/union_relation.rb', line 129 def add(relation, *sources) if columns.length != sources.length raise MismatchedColumnsError.new(columns, sources) end subqueries << Subquery.new(relation, sources) end |
#all ⇒ Object
Creates an ActiveRecord::Relation object that will pull all of the subqueries together.
139 140 141 142 143 144 |
# File 'lib/active_record/union_relation.rb', line 139 def all raise NoConfiguredSubqueriesError if subqueries.empty? model = subqueries.first.relation.model subclass_for(model).from(union_for(model)).select(discriminator, *columns) end |