Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A administrate
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 96
    • Issues 96
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 32
    • Merge requests 32
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • thoughtbot, inc.
  • administrate
  • Issues
  • #1758
Closed
Open
Issue created Sep 08, 2020 by Administrator@rootContributor

DEPRECATION WARNING: Dangerous query method

Created by: sejinkim1904

  • What were you trying to do?

    • Set a HasMany field as default sorting attribute
  • What did you end up with (logs, or, even better, example apps are great!)?

    • While running specs:
DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) 
called with non-attribute argument(s): "COUNT(jobs.id) desc". Non-attribute arguments will be disallowed 
in Rails 6.0. This method should not be called with user-provided values, such as request parameters or 
model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). 
# Administrate::Order

def apply(relation)
  return order_by_association(relation) unless
    reflect_association(relation).nil?

  order = "#{relation.table_name}.#{attribute} #{direction}"

  return relation.reorder(Arel.sql(order)) if 
  # Arel.sq() used here to prevent the deprecation message  
  # but not for #order_by_association
    relation.columns_hash.keys.include?(attribute.to_s)

  relation
end

def order_by_association(relation)
  return order_by_count(relation) if has_many_attribute?(relation) 
  # order_by_count triggered since sorting by HasMany field

  return order_by_id(relation) if belongs_to_attribute?(relation)

  relation
end

def order_by_count(relation)
  relation.
    left_joins(attribute.to_sym).
    group(:id).
    reorder("COUNT(#{attribute}.id) #{direction}")
    # Root of the deprecation warning. This raw SQL needs to be wrapped in Arel.sql()
end
  • What versions are you running?
    • Rails 5.2.4.3
    • administrate 0.14.0
Assignee
Assign to
Time tracking