alexbevi / redmine_knowledgebase

A knowledgebase plugin for Redmine
452 stars 199 forks source link

AmbiguousColumn Error "id" in knowledgebase/articles/authored #404

Closed jf912 closed 1 year ago

jf912 commented 1 year ago

Clicking the "Articles written by me: ###" link on the KB main page causes the following error in the production log:

`Completed 500 Internal Server Error in 18ms (ActiveRecord: 9.8ms)

ActiveRecord::StatementInvalid (PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous LINE 1: ...s ON kb_articles.id = taggings.taggable_id WHERE ("id" = NUL... ^ : SELECT tags.id, tags.name, COUNT() AS count FROM "tags" INNER JOIN taggings ON tags.id = taggings.tag_id INNER JO IN kb_articles ON kb_articles.id = taggings.taggable_id WHERE ("id" = NULL AND taggings.taggable_type = 'KbArticle') GROUP BY tags.id, tags.name HAVING (COUNT() > 0)):`

jf912 commented 1 year ago

I have fixed this with the following code changes:

diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb
index cfe8d00..a8d5a97 100644
--- a/app/controllers/articles_controller.rb
+++ b/app/controllers/articles_controller.rb
@@ -40,14 +40,14 @@ class ArticlesController < ApplicationController
   def authored

     @author_id = params[:author_id]
-    @articles = KbArticle.where(id: @project.articles.where(:author_id => @author_id).pluck(&:id))
+    @articles = KbArticle.where('kb_articles.id in (?)', @project.articles.where(:author_id => @author_id).pluck(:id))
                          .order("#{KbArticle.table_name}.#{sort_column} #{sort_direction}")

     if params[:tag]
       @tag = params[:tag]
       @tag_array = *@tag.split(',')
       @tag_hash = Hash[ @tag_array.map{ |tag| [tag.downcase, 1] } ]
-      @articles = KbArticle.where(id: @articles.tagged_with(@tag).map(&:id))
+      @articles = KbArticle.where('kb_articles.id in (?)', @articles.tagged_with(@tag).map(&:id))
     end

     @tags = @articles.tag_counts.sort { |a, b| a.name.downcase <=> b.name.downcase }