Class: RelationToJSON::BelongsToReflection
- Inherits:
-
BaseReflection
- Object
- BaseReflection
- RelationToJSON::BelongsToReflection
- Defined in:
- lib/relation_to_json/belongs_to_reflection.rb
Instance Attribute Summary
Attributes inherited from BaseReflection
#name, #reflection, #required_columns
Instance Method Summary collapse
Methods inherited from BaseReflection
#foreign_key, #initialize, #primary_key
Constructor Details
This class inherits a constructor from RelationToJSON::BaseReflection
Instance Method Details
#association_relation(transposed) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/relation_to_json/belongs_to_reflection.rb', line 21 def association_relation(transposed) if polymorphic? active_record.where( primary_key => transposed[foreign_key], ) else klass.where( primary_key => transposed[foreign_key], ) end end |
#pluck_association_columns(transposed) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/relation_to_json/belongs_to_reflection.rb', line 5 def pluck_association_columns(transposed) return recurse_json_with_schema(transposed) if nested_relations? required_columns = Set[primary_key, *@required_columns] plucked_attributes = association_relation(transposed) .pluck(*required_columns) .map { |plucked| required_columns.zip(Array.wrap(plucked)).to_h } primary_key_indexed_plucked_values = plucked_attributes .to_h { |attributes| [attributes[primary_key], attributes] } transposed.fetch(foreign_key, []).map do |record_primary_key| primary_key_indexed_plucked_values[record_primary_key] end end |