Skip to content

Commit

Permalink
Add notes for unscoped
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Jan 6, 2025
1 parent e592dc5 commit 504c583
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ def self.update_box_area_and_center_columns
# update the locations
update_all(update_center_and_area_sql)
# give center points to associated observations in batches by location_id
# Observation must be unscoped or it will not join to locations
# Observation must be unscoped in order to join to locations.
# (removing default_scope)
Observation.unscoped.in_box_of_max_area.group(:location_id).update_all(
location_lat: Location[:center_lat], location_lng: Location[:center_lng]
)
Expand Down
2 changes: 2 additions & 0 deletions app/models/name/taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ def best_matches_from_array(names, includes = [])
# Now allows includes, for batch lookup of Naming email interested parties
# GOTCHA: `search_name` cannot be used as a field in this AR where clause
def batch_lookup_all_matches(name_or_names, includes = [])
# Name must be unscoped in order to keep `or` condition comparable.
# (removing default_scope)
Name.unscoped.where(Name[:search_name].in(name_or_names)).
or(Name.unscoped.where(Name[:text_name].in(name_or_names))).
with_correct_spelling.includes(includes)
Expand Down
4 changes: 4 additions & 0 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ def self.refresh_content_filter_caches(dry_run: false)
def self.refresh_cached_column(type: nil, foreign: nil, local: foreign,
dry_run: false)
tbl = type.camelize.constantize.arel_table
# Observation must be unscoped in order to join to another table.
# (removing default_scope)
query = Observation.unscoped.joins(type.to_sym).
where(Observation[local.to_sym].not_eq(tbl[foreign.to_sym]))
msgs = query.map do |obs|
Expand All @@ -781,6 +783,8 @@ def self.refresh_cached_column(type: nil, foreign: nil, local: foreign,
# date. Fixes and returns a messages for each one that was wrong.
# Used by refresh_caches script
def self.make_sure_no_observations_are_misspelled(dry_run: false)
# Observation must be unscoped in order to join to name.
# (removing default_scope)
query = Observation.unscoped.joins(:name).
where(Name[:correct_spelling_id].not_eq(nil))
msgs = query.pluck(Observation[:id], Name[:text_name]).
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,14 @@ def in_group?(group)
end

# Return an Array of Project's that this User is an admin for.
# Project must be unscoped in order to join to another table.
# (removing default_scope)
def projects_admin
Project.unscoped.joins(:admin_group_users).where(user_id: id)
end

# Return an Array of Project's that this User is a member of.
# unscoped: see above
def projects_member(order: :created_at, include: nil)
@projects_member ||= Project.unscoped.where(user_group: user_groups.ids).
includes(include).order(order).to_a
Expand Down

0 comments on commit 504c583

Please sign in to comment.