我是相當新的軌道,所以我的傾向是臃腫的意見。 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")}