artist.rb: fix ambiguous column references.

Using `search[empty_only]=true` caused certain queries to throw an
exception due to ambiguous column references after joining on the tags
table.

Example:

    https://danbooru.donmai.us/artists?search[empty_only]=true&search[name]=hammer*

    PG::AmbiguousColumn exception raised
    ERROR: column reference "name" is ambiguous LINE 1: ...ags"."name" = "artists"."name" WHERE (true) AND ((name LIKE ... ^
    lib/danbooru/paginator/active_record_extension.rb:108:in `total_count'
    lib/danbooru/paginator/active_record_extension.rb:63:in `block in paginate_numbered'
    lib/danbooru/paginator/active_record_extension.rb:60:in `tap'
    lib/danbooru/paginator/active_record_extension.rb:60:in `paginate_numbered'
    lib/danbooru/paginator/active_record_extension.rb:15:in `paginate'
    app/controllers/artists_controller.rb:41:in `index'
This commit is contained in:
evazion
2017-04-07 16:53:27 -05:00
parent 674028588c
commit fbba167f0c

View File

@@ -21,6 +21,11 @@ class Artist < ActiveRecord::Base
attr_accessible :is_active, :as => [:builder, :janitor, :moderator, :default, :admin] attr_accessible :is_active, :as => [:builder, :janitor, :moderator, :default, :admin]
attr_accessible :is_banned, :as => :admin attr_accessible :is_banned, :as => :admin
scope :active, lambda { where(is_active: true) }
scope :deleted, lambda { where(is_active: false) }
scope :banned, lambda { where(is_banned: true) }
scope :unbanned, lambda { where(is_banned: false) }
module UrlMethods module UrlMethods
extend ActiveSupport::Concern extend ActiveSupport::Concern
@@ -328,14 +333,6 @@ class Artist < ActiveRecord::Base
end end
module SearchMethods module SearchMethods
def active
where("is_active = true")
end
def banned
where("is_banned = true")
end
def url_matches(string) def url_matches(string)
matches = find_all_by_url(string).map(&:id) matches = find_all_by_url(string).map(&:id)
@@ -362,33 +359,33 @@ class Artist < ActiveRecord::Base
def other_names_match(string) def other_names_match(string)
if string =~ /\*/ && CurrentUser.is_builder? if string =~ /\*/ && CurrentUser.is_builder?
where("other_names ILIKE ? ESCAPE E'\\\\'", string.to_escaped_for_sql_like) where("artists.other_names ILIKE ? ESCAPE E'\\\\'", string.to_escaped_for_sql_like)
else else
where("other_names_index @@ to_tsquery('danbooru', E?)", Artist.normalize_name(string).to_escaped_for_tsquery) where("artists.other_names_index @@ to_tsquery('danbooru', E?)", Artist.normalize_name(string).to_escaped_for_tsquery)
end end
end end
def group_name_matches(name) def group_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like stripped_name = normalize_name(name).to_escaped_for_sql_like
where("group_name LIKE ? ESCAPE E'\\\\'", stripped_name) where("artists.group_name LIKE ? ESCAPE E'\\\\'", stripped_name)
end end
def name_matches(name) def name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like stripped_name = normalize_name(name).to_escaped_for_sql_like
where("name LIKE ? ESCAPE E'\\\\'", stripped_name) where("artists.name LIKE ? ESCAPE E'\\\\'", stripped_name)
end end
def named(name) def named(name)
where("name = ?", normalize_name(name)) where(name: normalize_name(name))
end end
def any_name_matches(name) def any_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like stripped_name = normalize_name(name).to_escaped_for_sql_like
if name =~ /\*/ && CurrentUser.is_builder? if name =~ /\*/ && CurrentUser.is_builder?
where("(name LIKE ? ESCAPE E'\\\\' OR other_names LIKE ? ESCAPE E'\\\\')", stripped_name, stripped_name) where("(artists.name LIKE ? ESCAPE E'\\\\' OR artists.other_names LIKE ? ESCAPE E'\\\\')", stripped_name, stripped_name)
else else
name_for_tsquery = normalize_name(name).to_escaped_for_tsquery name_for_tsquery = normalize_name(name).to_escaped_for_tsquery
where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', E?))", stripped_name, name_for_tsquery) where("(artists.name LIKE ? ESCAPE E'\\\\' OR artists.other_names_index @@ to_tsquery('danbooru', E?))", stripped_name, name_for_tsquery)
end end
end end
@@ -413,7 +410,7 @@ class Artist < ActiveRecord::Base
q = q.banned q = q.banned
when /status:active/ when /status:active/
q = q.where("is_banned = false and is_active = true") q = q.unbanned.active
when /./ when /./
q = q.any_name_matches(params[:name]) q = q.any_name_matches(params[:name])
@@ -422,23 +419,23 @@ class Artist < ActiveRecord::Base
params[:order] ||= params.delete(:sort) params[:order] ||= params.delete(:sort)
case params[:order] case params[:order]
when "name" when "name"
q = q.reorder("name") q = q.reorder("artists.name")
when "updated_at" when "updated_at"
q = q.reorder("updated_at desc") q = q.reorder("artists.updated_at desc")
else else
q = q.reorder("id desc") q = q.reorder("artists.id desc")
end end
if params[:is_active] == "true" if params[:is_active] == "true"
q = q.active q = q.active
elsif params[:is_active] == "false" elsif params[:is_active] == "false"
q = q.where("is_active = false") q = q.deleted
end end
if params[:is_banned] == "true" if params[:is_banned] == "true"
q = q.banned q = q.banned
elsif params[:is_banned] == "false" elsif params[:is_banned] == "false"
q = q.where("is_banned = false") q = q.unbanned
end end
if params[:id].present? if params[:id].present?