私は、メイン画面上のアイデアをソートする必要があるアプリケーション、持っている:降順の日付ではアイデア 二つの注文パラメータ - レール
- を
これは私のコントローラです:
def telaPrincipal
@listOfIdeas = Idea.all
end
そして、これは私のHTMLです:
<% @listOfIdeas.each do |t| %>
<tr>
<td><%= t.created_at.strftime("%d/%m/%y")%></td>
<td><%= link_to t.title, t.link_to_idea.match(/^.*:(:\/\/)/) ? t.link_to_idea : "http://#{t.link_to_idea}", :target => "_blank" %></td>
<%if (isIdeaVotedByUser t, current_user.id)%>
<td>
<%= link_to image_tag("like-button.png", border: 0), {action: 'destroy', controller: 'votes', id: t.votes.where(:users_id => current_user.id).pluck(:id).first}, data:{confirm:"Tem certeza que deseja cancelar o seu voto?"},
:method => :delete, class: 'like-button'%>
</td>
<%else %>
<td>
<%= link_to image_tag("semlike-button.png", border: 0), {action: 'create', controller: 'votes' , idea_id: t.id , idea_user: t.users_id},
:method => :post, class: 'like-button'%>
</td>
<% end %>
<td>
<%= t.votes.count%>
</td>
私はこのような何かを考えていた:あなたは私が合計にt.votes.count
を使用し、見ることができるように、メイン画面でIdeas.order ('created_at: desc'; 'votes desc')
。
OBS:クラスのアイデア...
class Idea < ActiveRecord::Base
has_many :votes , :dependent => :destroy
belongs_to :user
end
Votes
は私のDB内のテーブルです。
私はレールに新たなんだと私は
私のRailsをいわばこれらのクエリをネイティブにする方法がわからない5.1.2
感謝です!
特にhttps://stackoverflow.com/a/31378899/525478 –
[OK]を、私はunderstanded日付が、どのように私はundestandedいない票の合計を作ります。私が使用することはできませんので : @listOfIdeas = Idea.all.order(のcreated_at:DESC、投票:DESC)は は、少なくともそれは 'votes'が関係ある場合には動作しません –
を動作しませんでした。 https://stackoverflow.com/questions/25487098/order-an-activerecord-relation-objectデフォルトで投票でソートすることはできますが、アソシエーション宣言 – whodini9