2014-10-01 98 views
2

我是相當新的軌道,所以我的傾向是臃腫的意見。 rack-mini-profiler顯示我的加載時間的2/3以及來自渲染我的視圖的1/2我的sql調用。我正在尋找關於如何清潔了一些建議,以及爲更有效地編碼應用一些好的資源。清理臃腫意見

我有旅行的用戶(:city_id,:起始日期,時間:END_DATE)這些旅行belongs_to的一個城市,一個belongs_to的國家。

控制器

# this grabs topics that are concidered genral 
@feed = Topic.where(:city_id => current_user.trips.pluck(:city_id), :general_topic => true).uniq 
# topics that are specific to a users trip time range 
current_user.trips.each do |trip| 
    @feed += Topic.where("(general_topic = ? AND city_id = ?) AND ((start_date BETWEEN ? AND ? OR end_date BETWEEN ? AND ?) OR (start_date <= ? AND end_date >= ?))", false, trip.city_id, trip.start_date, trip.end_date, trip.start_date, trip.end_date, trip.start_date, trip.end_date).uniq 
end 

視圖

<% @feed.each do |topic| %> 
<% if topic.general_topic %> 
    <div> 
     <div> 
      <h3> 
       <%= link_to(topic) do %>  
        <%= topic.title %> 
       <% end %> 
      </h3> 
     </div> 
     <div> 
     <p><%= time_ago_in_words(topic.created_at) %> by <%= link_to topic.user.name, "#" %> about <%= topic.city.name %>, <%= topic.city.country.name %></p> 
     </div> 
     <div> 
      <p><%= topic.comments.size %> comments 
       <% if topic.comments.any? %> 
       (<%= time_ago_in_words(topic.comments.last.updated_at) %> ago) 
       <% end %></p> 
     </div>  
    </div>  
<% else %> 
    <div> 
     <div> 
      <h3> 
       <%= link_to(topic) do %>  
        <%= topic.title %> 
       <% end %> 
      </h3> 
     </div> 
     <div> 
     <p><%= time_ago_in_words(topic.created_at) %> by <%= link_to topic.user.name, "#" %> in <%= topic.city.name %>, <%= topic.city.country.name %> <%= topic.start_date.strftime("%m/%d") %>-<%= topic.end_date.strftime("%m/%d") %></p> 
     </div> 
     <div> 
      <p><%= topic.comments.size %> comments 
       <% if topic.comments.any? %> 
       (<%= time_ago_in_words(topic.comments.last.updated_at) %> ago) 
       <% end %></p> 
     </div> 
    </div>  
<% end %>  
<% end %> 
</div> <!-- end discussions --> 

我知道這是很可怕的。但我想的話題是這個樣子:

Topic.Title 
topic.created_at "by" topic.user.name "about" topic.city.name "," topic.city.country.name 
topic.comments.count (topic.comment.last.created_at) 

我應該開始尋找到爲了得到一些邏輯到控制器或模型? 目前我的主題模型就是:

class Topic < ActiveRecord::Base 
belongs_to :user 
belongs_to :trip 
belongs_to :city 
has_many :comments, :class_name => 'Comment', :foreign_key => :topic_id 
default_scope { order("created_at DESC")} 

回答

0

有幾件事情可以做,速度和可讀性。

對於速度,在控制器渴望加載數據,使用includes。例如,當你加載的主題,你可以加載相關評論過:

@feed = Topic.where(:city_id => current_user.trips.pluck(:city_id), :general_topic => true).includes(:comments) 

看到Rails 4: How to use includes() with where() to retrieve associated objects

一般情況下,應儘量減少量的數據庫調用你在你的意見辦:當它在的完成控制器,它都在一個地方完成,易於閱讀和維護。

爲了增強可讀性,你可以幹(「不重複自己」),你的代碼。看看if和else塊的內容:它們幾乎是相同的:告訴你一個額外的行,如果話題是不是.general_topic。爲什麼重複這麼多相同的東西?如果存在

<% @feed.each do |topic| %> 
    <div> 
    <div> 
     <h3> 
     <%= link_to(topic) do %>  
      <%= topic.title %> 
     <% end %> 
     </h3> 
    </div> 
    <div> 
     <p> 
     <%= time_ago_in_words(topic.created_at) %> by <%= link_to topic.user.name, "#" %> about <%= topic.city.name %>, <%= topic.city.country.name %> 
     <% unless topic.general_topic %> 
      <%= topic.start_date.strftime("%m/%d") %>-<%= topic.end_date.strftime("%m/%d") %> 
     <% end %>   
     </p> 
    </div> 
    <div> 
     <p><%= topic.comments.size %> comments 
     <% if last = topic.comments.last %> 
      (<%= time_ago_in_words(last.updated_at) %> ago) 
     <% end %> 
     </p> 
    </div>  
    </div> 
<% end %>  

注意,我刪除topic.comments.any?,並與一些代碼來獲取最後一個更換它,測試:你會因此改寫。這是句法糖(一旦渴望加載發生),但在我看來更清晰。