Rename columns with a slash in their names

These columns were causing Rails 5.2 to throw a warning when ordering by
them, as if they weren't valid column names:

DEPRECATION WARNING: Dangerous query method (method whose arguments are
used as raw SQL) called with non-attribute argument(s):
:"budget/investments_count". 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().

This change also makes their names consistent with the rest of our
tables and columns.
This commit is contained in:
Javi Martín
2020-05-17 23:12:06 +02:00
parent 6fd9a286d7
commit d7d421b88f
4 changed files with 17 additions and 8 deletions

View File

@@ -31,6 +31,6 @@ class TagCloud
end end
def table_name def table_name
resource_model.to_s.downcase.pluralize.gsub("::", "/") resource_model.to_s.tableize.tr("/", "_")
end end
end end

View File

@@ -79,7 +79,7 @@ module ActsAsTaggableOn
private private
def custom_counter_field_name_for(taggable_type) def custom_counter_field_name_for(taggable_type)
"#{taggable_type.underscore.pluralize}_count" "#{taggable_type.tableize.tr("/", "_")}_count"
end end
end end
end end

View File

@@ -0,0 +1,9 @@
class RenameColumnsWithSlashCharacters < ActiveRecord::Migration[5.1]
def change
change_table :tags do |t|
t.rename :"budget/investments_count", :budget_investments_count
t.rename :"legislation/proposals_count", :legislation_proposals_count
t.rename :"legislation/processes_count", :legislation_processes_count
end
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20191108173350) do ActiveRecord::Schema.define(version: 20200519120717) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -1407,12 +1407,12 @@ ActiveRecord::Schema.define(version: 20191108173350) do
t.integer "debates_count", default: 0 t.integer "debates_count", default: 0
t.integer "proposals_count", default: 0 t.integer "proposals_count", default: 0
t.string "kind" t.string "kind"
t.integer "budget/investments_count", default: 0 t.integer "budget_investments_count", default: 0
t.integer "legislation/proposals_count", default: 0 t.integer "legislation_proposals_count", default: 0
t.integer "legislation/processes_count", default: 0 t.integer "legislation_processes_count", default: 0
t.index ["debates_count"], name: "index_tags_on_debates_count" t.index ["debates_count"], name: "index_tags_on_debates_count"
t.index ["legislation/processes_count"], name: "index_tags_on_legislation/processes_count" t.index ["legislation_processes_count"], name: "index_tags_on_legislation_processes_count"
t.index ["legislation/proposals_count"], name: "index_tags_on_legislation/proposals_count" t.index ["legislation_proposals_count"], name: "index_tags_on_legislation_proposals_count"
t.index ["name"], name: "index_tags_on_name", unique: true t.index ["name"], name: "index_tags_on_name", unique: true
t.index ["proposals_count"], name: "index_tags_on_proposals_count" t.index ["proposals_count"], name: "index_tags_on_proposals_count"
end end