Module: Organizations::Sharding

Extended by:
ActiveSupport::Concern
Included in:
ApplicationRecord
Defined in:
app/models/concerns/organizations/sharding.rb

Instance Method Summary collapse

Instance Method Details

#sharding_organizationObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/concerns/organizations/sharding.rb', line 13

def sharding_organization
  self.class.sharding_keys.reduce(nil) do |found, (column, table)|
    next found unless ApplicationRecord.connection.data_source_exists?(table)

    record_id = attributes[column]
    next found unless record_id

    org = if table == 'organizations'
            ::Organizations::Organization.find_by(id: record_id)
          else
            ::Organizations::Organization.joins(table.to_sym).find_by(table => { id: record_id })
          end

    next found unless org

    next nil if found && found.id != org.id

    org
  end
end