Vieenay Siingh
Vieenay Siingh

Reputation: 867

displaying related articles according to tag in ruby on rails

I am trying to show related articles on show page of article. When user views any particular article, all related articles in db should be displayed on that page ( in right side bar) according tag( as every article has at-least one tag).My app has relation between tag and article (please below).

articles_controller.rb

class ArticlesController < ApplicationController

  before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
    def is_user_admin
      redirect_to(action: :index) unless current_user.try(:is_admin?) 
      return false 
    end

    def index
      @articles = Article.all(:order => "created_at DESC")
      @article_titles = Article.first(10)

      @tags = Tag.all
    end

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

    def new
      @article = Article.new
    end

    def create
      @article = Article.new(params[:article])
      @article.user_id = current_user.id

      if @article.save
        flash[:success] = "article created!"
        redirect_to article_path(@article)

      else
        render 'new' 
      end 
    end

    def destroy
      @article = Article.find(params[:id])
      @article.destroy

      redirect_to action:  'index'  
    end

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

    def update
      @article = Article.find(params[:id])

      if @article.update_attributes(params[:article])
       flash.notice = "Article '#{@article.title}' Updated!"
       redirect_to article_path(@article)

      else 
        render 'edit'
      end
  end
end

tags_controller.rb

class TagsController < ApplicationController
  #before_filter :user_signed_in, only: [:destroy]

  def index
    @tags = Tag.all
  end

  def show
    @tag = Tag.find(params[:id])
  end
end

schema.rb

ActiveRecord::Schema.define(:version => 20130411074056) do
  create_table "articles", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "user_id"
  end

  create_table "comments", :force => true do |t|
    t.text     "content"
    t.integer  "user_id"
    t.string   "article_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "taggings", :force => true do |t|
    t.integer  "tag_id"
    t.integer  "article_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "taggings", ["article_id"], :name => "index_taggings_on_article_id"
  add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"

  create_table "tags", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

articles/show.html.erb :- where i need to display related articles when user views any particular article based on tag

Currently articles/show.html.erb page has tag(S) name and I want to display all artilces with same tag in db to display on this page (right side bar). Any idea how to implement this relation to fetch out related articles and where to write this logic and how to implement in views.

Upvotes: 1

Views: 2210

Answers (1)

jvnill
jvnill

Reputation: 29599

If you are using acts_as_taggable_on, what you want is easy to achieve using the following code

def show
  @article = Article.find(params[:id])
  @related_articles = Article.tagged_with(@article.tag_list, any: true)
end

Since you rolled up your own implementation, you'd have to manually do the fetch.

First get a list of all the tags of the article. That would be @article.tag_ids.

Next, get a list of articles associated to those tag ids

@related_articles = Article
  .joins(:taggings)
  .where('articles.id != ?', @article.id)
  .where(taggings: { tag_id: @article.tag_ids })

Upvotes: 7

Related Questions