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
25 26 27 28 29 |
# File 'lib/prim/instance_methods.rb', line 25 def only_one_primary if self[:primary] siblings.update_all('"primary" = false') 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).
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/prim/instance_methods.rb', line 35 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 |