0

I can't understand why this is happening, seems that id of article is being queried even though friendly.id has been found. ID being found is "0" for any article that i click, even though the correct article_id (59) is found when associated with a comment.

Processing by ArticlesController#show as HTML
  Parameters: {"id"=>"javascript-8"}
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Article Load (0.7ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."slug" = $1 LIMIT $2  [["slug", "javascript-8"], ["LIMIT", 1]]
   (1.9ms)  BEGIN
  Article Load (0.3ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 0], ["LIMIT", 1]]
   (1.1ms)  ROLLBACK
  Rendering articles/show.html.erb within layouts/application
  Rendered comments/_comment_form.html.erb (19.3ms)
  Comment Load (0.7ms)  SELECT "comments".* FROM "comments" WHERE "comments"."article_id" = 59 ORDER BY created_at DESC
  Rendered comments/_comment.html.erb (25.2ms)

EDITED: Articles controller

class ArticlesController < ApplicationController
    before_action :authenticate_user!
    before_action :set_article, only: [:show, :edit, :update, :destroy, :toggle_vote]
    impressionist :actions=>[:show]

  def show
    @article_categories = @article.categories
    @comments = Comment.where(article_id: @article).order("created_at DESC")

    if @article.status == "draft" && @article.user != current_user
      redirect_to root_path
    end
  end

  private

    def set_article
      @article = Article.friendly.find(params[:id])
    end

end
krishnar
  • 2,537
  • 9
  • 23
doyz
  • 887
  • 2
  • 18
  • 43

2 Answers2

0

friendly_id :foo, use: :slugged # you must do MyClass.friendly.find('bar')

or...

friendly_id :foo, use: [:slugged, :finders] # you can now do MyClass.find('bar')

In your case

def set_article
  @article = Article.friendly.find(params[:id])
end
krishnar
  • 2,537
  • 9
  • 23
0

It was because i was using impressionist gem.

I should remove

impressionist :actions=>[:show] 

at top of controller, and instead add this in show

def show
  impressionist(@article)
end

As written in impressionist gem usage guide (https://github.com/charlotte-ruby/impressionist#usage), "f you're using friendly_id be sure to log impressionist this way, as params[:id] will return a string(url slug) while impressionable_id is a Integer column in database. "

doyz
  • 887
  • 2
  • 18
  • 43