Class: GemExtensions::ActiveRecord::DisableJoins::Relation
- Inherits:
-
ActiveRecord::Relation
- Object
- ActiveRecord::Relation
- GemExtensions::ActiveRecord::DisableJoins::Relation
- Defined in:
- lib/gem_extensions/active_record/disable_joins/relation.rb
Instance Attribute Summary collapse
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #first(limit = nil) ⇒ Object
-
#initialize(klass, key, ids) ⇒ Relation
constructor
A new instance of Relation.
- #limit(value) ⇒ Object
- #load ⇒ Object
Constructor Details
#initialize(klass, key, ids) ⇒ Relation
Returns a new instance of Relation.
9 10 11 12 13 |
# File 'lib/gem_extensions/active_record/disable_joins/relation.rb', line 9 def initialize(klass, key, ids) @ids = ids.uniq @key = key super(klass) end |
Instance Attribute Details
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
7 8 9 |
# File 'lib/gem_extensions/active_record/disable_joins/relation.rb', line 7 def ids @ids end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/gem_extensions/active_record/disable_joins/relation.rb', line 7 def key @key end |
Instance Method Details
#first(limit = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/gem_extensions/active_record/disable_joins/relation.rb', line 19 def first(limit = nil) if limit records.limit(limit).first else records.first end end |
#limit(value) ⇒ Object
15 16 17 |
# File 'lib/gem_extensions/active_record/disable_joins/relation.rb', line 15 def limit(value) records.take(value) # rubocop:disable CodeReuse/ActiveRecord end |
#load ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gem_extensions/active_record/disable_joins/relation.rb', line 27 def load super records = @records records_by_id = records.group_by do |record| record[key] end records = ids.flat_map { |id| records_by_id[id.to_i] } records.compact! @records = records end |