按大多数视图排序的索引
Posted
技术标签:
【中文标题】按大多数视图排序的索引【英文标题】:Index Sorted by Most Views 【发布时间】:2014-06-10 05:53:15 【问题描述】:我无法按大多数视图对“图钉”索引进行排序。该视图未显示任何已排序的“图钉”。当用户访问 pin 时,它会将数据存储在“visit_details”和“visits”表中。在模型中,我具有处理排序的功能,但我不确定这是问题所在还是发生了什么。
create_table "visit_details", force: true do |t|
t.integer "visit_id"
t.string "ip_address", limit: 15
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "visit_details", ["ip_address"], name: "index_visit_details_on_ip_address"
create_table "visits", force: true do |t|
t.integer "total_visits"
t.integer "unique_visits"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "pin_id"
end
add_index "visits", ["pin_id"], name: "index_visits_on_pin_id"
我的控制器:
class PinMostViewsController < ApplicationController
def index
@random_model = Pin.order('random()').first
@pins = Pin.top_viewed(params[:page], params[:date])
end
def pin_most_views_params
params.require(:pin).permit(:ip_address, :visit_id)
end
end
我的 pin.rb 模型存储逻辑:
class Pin < ActiveRecord::Base
belongs_to :user
acts_as_commentable
has_attached_file :image, :styles => :medium => "300X300>", :thumb => "100X100>"
validates :image, presence: true
validates :description, presence: true
validates_attachment :image, content_type: content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"]
has_one :visit
validates :team, presence: true
validates :position, presence: true
def self.top_viewed(page, time_period)
case time_period
when "all_time"
Pin.all_time(page)
when "year"
Pin.order_most_viewed(page, 1.year.ago)
when "month"
Pin.order_most_viewed(page, 1.month.ago)
when "week"
Pin.order_most_viewed(page, 1.week.ago)
when "day"
Pin.order_most_viewed(page, 1.day.ago)
else
Pin.all_time(page)
end
end
def self.order_most_viewed(page, date)
visits, ids = , []
# Create a hash containing pins and their respective visit numbers
VisitDetail.includes(:visit).where('created_at >= ?', date).each do |visit_detail|
pin_id = visit_detail.visit.pin_id.to_s
if visits.has_key?(pin_id)
visits[pin_id] += 1
else
visits[pin_id] = 1
end
end
if visits.blank?
# Since no visits existed for this time period, we simply return an empty array
# which will display no results on the view page
[]
else
# Now we sort the pins from most views to least views
visits.sort_by |k,v| v .reverse.each |k, v| ids << k
# With our array of ids, we get all of the pins in the particular order
Pin.page(page).per_page(30).where(id: ids).order_by_ids(ids)
end
end
# A nice query method that will sort by ids, used for the order_most_viewed class method above
def self.order_by_ids(ids)
order_by = ["case"]
ids.each_with_index.map do |id, index|
order_by << "WHEN id='#id' THEN #index"
end
order_by << "end"
order(order_by.join(" "))
end
def self.all_time(page)
Pin.includes(:visit)
.where('visits.id IS NOT NULL')
.order("visits.total_visits DESC")
.order("visits.total_visits > 0")
.page(page).per_page(30)
end
# Instance Methods
# ================
def image_remote_url=(url_value)
self.image = URI.parse(url_value) unless url_value.blank?
super
end
def previous
self.class.first(:conditions => ["id < ?", id], :order => "id desc")
end
def next
self.class.first(:conditions => ["id > ?", id], :order => "id asc")
end
end
最后,我的观点(应该按大多数观点排序的图钉索引)
<div id="pins" class="transitions-enabled">
<% @pins.each do |pin| %>
<div class="box panel panel-default">
<%= link_to image_tag(pin.image.url(:medium)), pin %>
<div class="panel-body">
<p><%= pin.description %></p>
<p><strong><%= pin.user.name if pin.user %></strong></p>
<% if current_user && pin.user == current_user %>
<div class="actions">
<%= link_to edit_pin_path(pin) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<%= link_to pin, method: :delete, data: confirm: 'Are you sure?' do %>
<span class="glyphicon glyphicon-trash"></span>
Delete
<% end %>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
【问题讨论】:
+1 用于使用case
/ switch
;)
您是否尝试过在控制台中探测模型?启动它并尝试你的模型方法,特别是Pin.top_viewed(..)
。尝试不同的值,看看它返回什么。
【参考方案1】:
抱歉,我还不能发表评论。有没有尝试追踪它?
您可以在 html 中使用 p
来查看发生了什么。
您也可以在控制器端使用 logger.info。
因此,您将看到,您的问题隐藏在哪里。当你看到nil
或[]
【讨论】:
以上是关于按大多数视图排序的索引的主要内容,如果未能解决你的问题,请参考以下文章