Class: AssociationScope::Scope::BelongsToReflection
- Inherits:
-
AssociationScope::Scope
- Object
- AssociationScope::Scope
- AssociationScope::Scope::BelongsToReflection
- Defined in:
- lib/association_scope/scope/belongs_to_reflection.rb
Instance Attribute Summary
Attributes inherited from AssociationScope::Scope
Instance Method Summary collapse
Methods inherited from AssociationScope::Scope
Constructor Details
This class inherits a constructor from AssociationScope::Scope
Instance Method Details
#apply ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/association_scope/scope/belongs_to_reflection.rb', line 6 def apply if reflection_details.[:polymorphic] raise PolymorphicAssociationError.new association: association, model: model end association = @association class_name = reflection_details.[:class_name]&.constantize || association.camelize.constantize foreign_key = reflection_details.[:foreign_key] association_name = association.to_s.underscore.to_sym own_table_name = class_name.to_s.pluralize.underscore table_name = table_name class_name model.class_eval <<-RUBY, __FILE__, __LINE__ + 1 scope association.pluralize, -> do if foreign_key.present? class_name .joins("JOIN #{table_name} ON #{table_name}.#{foreign_key} = #{own_table_name}.id") else class_name .joins(table_name) end .where(table_name => { association_name => select("#{association_name}_id".to_sym) }) .distinct end RUBY end |