我有一個包含多個「源」的頁面。在我的控制器我有這樣的事情:Rails:從多個模型創建「所有活動源」
def index
@plays = current_user.plays.includes(:game).order("created_at desc")
@wants = current_user.wants.includes(:game).order("created_at desc")
@ratings = current_user.ratings.includes(:game).order("created_at desc")
end
我用這樣的代碼視圖:
<% @plays.each do |play| %>
You played <%= play.game.name %></p>
<% end %>
現在我想打一個,即「所有活動」頁面上來回飼料其中按照創建日期(desc)排序,在一個Feed中顯示Plays,Wants和Ratings。
任何幫助最好的方式來做到這一點會很好。
更新:如從每個模型請求的代碼,簡單好用的現在:
class Play < ActiveRecord::Base
attr_accessible :game_id, :user_id, :description
belongs_to :user
belongs_to :game
end
class Rating < ActiveRecord::Base
attr_accessible :game_id, :id, :score, :user_id, :description
belongs_to :user
belongs_to :game
end
class Want < ActiveRecord::Base
attr_accessible :game_id, :user_id, :description
belongs_to :user
belongs_to :game
end
您能告訴我們如何在用戶模型中定義戲劇,想要和評分方法嗎?它們是完全獨立的連接模型,範圍,自定義關聯方法嗎? –
@PeterDuijnstee更新了型號代碼 –