我需要用戶獲得5篇最受關注的文章。爲此,我實現了「印象」功能,並且文章和印象模型之間具有多態關係。使用此功能,我無法獲得每篇文章從用戶處獲得的意見。但現在我需要從db中獲取5個最常見的文章。我知道這是一個愚蠢的問題,但我試過了,找不到答案。如何在紅寶石軌道上獲得5篇最受關注的文章
schema.rb(我還沒有粘貼完整的文件,但只有相關的)
ActiveRecord::Schema.define(:version => 20130419101017) 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 "impressions", :force => true do |t|
t.string "impressionable_type"
t.integer "impressionable_id"
t.integer "user_id"
t.string "ip_address"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
articles_controller.rb
class ArticlesController < ApplicationController
before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
before_filter :log_impression, :only=> [:show]
def is_user_admin
redirect_to(action: :index) unless current_user.try(:is_admin?)
return false
end
def log_impression
@article = Article.find(params[:id])
# this assumes you have a current_user method in your authentication system
@article.impressions.create(ip_address: request.remote_ip,user_id:current_user.id)
end
def index
@articles = Article.all(:order => "created_at DESC")
@article_titles = Article.first(10)
@article_popular = Article.
@tags = Tag.all
end
def show
@article = Article.find(params[:id])
@related_articles = Article.joins(:taggings).
where('articles.id != ?', @article.id)
.where(taggings: { tag_id: @article.tag_ids })
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
article.rb
class Article < ActiveRecord::Base
attr_accessible :title, :body
attr_accessible :tag_list
has_many :comments
belongs_to :user
has_many :taggings
has_many :tags, through: :taggings
has_many :impressions, :as=>:impressionable
validates :title, :body, :tag_list, :presence => true
def impression_count
impressions.size
end
def unique_impression_count
impressions.group(:ip_address).size #UNTESTED: might not be correct syntax
end
def tag_list
self.tags.collect do |tag|
tag.name
end.join(", ")
end
def tag_list=(tags_string)
tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by_name(name) }
self.tags = new_or_found_tags
end
end
impression.rb
class Impression < ActiveRecord::Base
attr_accessible :impressionable_id, :impressionable_type, :ip_address, :user_id
belongs_to :impressionable, :polymorphic=>true
end
篇
文章/ show.html.erb
Viewed: <%[email protected]_count %> times
我這是怎麼計算沒有意見,從用戶。現在我想獲得用戶最喜歡的5篇文章。 任何幫助將升值。欲瞭解更多信息,我已經使用(simple hit counter for page views in rails)獲取用戶視圖。
嗨吸血鬼,感謝您的建議。我需要更多的信息。如果我實施了您的建議,我是否需要更改現有的展示次數設置。有什麼建議麼。 – 2013-04-21 10:58:56
不是我所見......計數器緩存將在您的控制器中通過「@ article.impressions.create」調用進行更新。但是,您可以/應該溝通的是您的「Article」和「Comment」模型中的「impression_count」方法 - 因爲您將使用計數器緩存列。我也認爲這將有利於性能,因爲你可以簡單地把訂單留給數據庫,不需要在ruby中做... – Vapire 2013-04-21 11:06:02
ps:你真的應該在導軌指南中讀到這種可能性,儘管.. – Vapire 2013-04-21 11:07:09