2014-06-10 49 views
1

我在排序大多數視圖的「大頭針」索引時遇到了問題。該視圖沒有顯示任何已排序的「引腳」。當用戶訪問一個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 

最後,我的觀點(即應該被大多數排序銷指數views)

<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> 
+0

+1使用'case' /'switch';) –

+0

你試過在控制檯探測模型嗎?啓動它並嘗試使用模型方法,特別是'Pin.top_viewed(..)'。嘗試不同的值,看看它返回的結果。 –

回答

0

對不起,我還不能評論。試過跟蹤它嗎? 您可以在html中使用p來查看發生了什麼。 你也可以在控制器端使用logger.info。 因此,您會看到問題隱藏的地方。當你看到nil[]