Module: Prim::InstanceMethods::Reflected
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/prim/instance_methods.rb
Instance Method Summary collapse
- #only_one_primary ⇒ Object
-
#siblings ⇒ Object
Builds a query selecting all siblings for a given mapping record.
Instance Method Details
#only_one_primary ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/prim/instance_methods.rb', line 31 def only_one_primary if self[:primary] siblings.update_all('"primary" = false') elsif siblings.where( primary: true ).first.nil? self[:primary] = true end end |
#siblings ⇒ Object
Builds a query selecting all siblings for a given mapping record. A record's
siblings are any records in the table of the reflected_class that match
the given record's foreign key and, if the association is polymorphic,
its foreign type. The set of siblings excludes self (this record).
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/prim/instance_methods.rb', line 43 def siblings foreign_key = prim_relationship.mapping_reflection.foreign_key mapping_type = prim_relationship.mapping_reflection.type primary_key = prim_relationship.reflected_class.primary_key # Select all by foreign key first, to handle all cases. query = self.class.where( foreign_key => self[ foreign_key ] ) # Only select by a foreign "type" column if one is used on the `reflected_class`, # making it a polymorphic association. unless mapping_type.nil? query = query.where( mapping_type => self[ mapping_type ] ) end # Exclude this record from the query. query.where( self.class.arel_table[ primary_key ].not_eq( self[ primary_key ] ) ) end |