2016-11-29 15 views
0

多くのロジックをビューからコントローラ/モデルに移動する方法を理解しようとしています。ビューからコントローラへの関数の移動Rails 5

(私はモデルに行くことになっていると思う?)

Show.html.erb

<div class="collectionList"> 
    <% @user.collections.each do |d| %> 
     <%= d.name %> 
     Distinct Cards: <%= d.card_collections.count %> <br /> 
     Total Cards: 
     <% d.card_collections.each do |x| %> 
     <% y = [] %> 
     <% y << x.card_counts %> 
     <% y.flatten! %> 
     <% y = y.inject(0){|sum,x| sum + x } %> 
     <%= y %> 
     <% end %> 
     <% if d.public %> Public <% end %><br /> 
    <% end %> 
    </div> 

すべてのユーザは、各コレクションはcard_collectionを持つことができ、コレクション、それぞれを持っていますそれらのうちの1つはカードを持つことができます。

「card_collections」の量は、あなたのデッキに別々のカードの量です。 「card_collections.card_counts」の合計は、カードの合計額です。私はそれを私はそれのように呼び出すことができますが、私はどのようにわからないモデルで 'カードの合計'と 'カードの数'としてそれを定義すると思っています。

また、私はより高速な結果を得るために摘む/合計を使用することができますので、私がモデルにそれを動かすことができれば、私のロジックが容易になるだろうと思いますか?

コレクション

class Collection < ApplicationRecord 
    belongs_to :user 
    has_many :card_collections 
    has_many :cards, through: :card_collections 

    # validates :user_id, presence: true 
end 

カード

class Card < ApplicationRecord 
    has_many :card_collections 
    has_many :collections, through: :card_collections 
    belongs_to :deck, optional: true 
end 

ここCardCollection

class CardCollection < ApplicationRecord 
    belongs_to :collection 
    belongs_to :card 
end 
+0

あなたは、テーブル間の関係(ではあり-多くが属する-に構造)、コントローラとモデルのフィールドでshowメソッドを説明してくださいすることができます。 –

答えて

0

、ちょうど次のCを追加お使いのコントローラでODE:あなたのビューファイルで

@user = User.joins(:collections).where("ANY CONDITION").select("*,count(collections.id) as card_collections_count, sum(collections.card_counts) as card_counts") 

<div class="collectionList"> 
    <% @user.each do |d| %> 
     <%= d.name %> 
     Distinct Cards: <%= d.card_collections_count %> <br /> 
     Total Cards: <%= d.card_counts %> 
     <% if d.public %> Public <% end %><br /> 
    <% end %> 
    </div> 

コーディングハッピー!

+0

私はそれがユーザコントローラだと仮定したので、あなたのクエリを追加しましたが、今すぐ取得します 'SQLite3 :: SQLException:そのような列はありません:collections.card_counts:card_counts as card_collections_count、sum(collections.card_counts) FROM "users" INNER JOIN "コレクション" ON "collections"。 "user_id" = "users"。 "id" ' お返事ありです。私は '@users = User.joins(:collections、:card_collections).select(" *、count(card_collections.id)as card_collections_count、sum(card_collections.card_counts)as card_counts ")'を試してみました。 。 – DNorthrup

+0

ここにあなたの参加エラーログを貼り付けてください – user100693

0

あなたは

class CardCollection < ApplicationRecord 
    belongs_to :collection 
    belongs_to :card 

    def sum_of_cards 
    y = [] 
    y << self.card_counts 
    y.flatten! 
    y = y.inject(0) { |sum, self| sum + self } 
    return y 
    end 
end 

としてあなたCardCollectionモデルにそれを行うことができ、その後、私が活用しSum機能を使用する方法を見つけ出すことができました

<div class="collectionList"> 
    <% @user.collections.each do |d| %> 
    <%= d.name %> 
    Distinct Cards: <%= d.card_collections.count %> <br /> 
    Total Cards: 
     <% d.card_collections.each do |x| %> 
     <%= x.sum_of_cards %> 
     <% end %> 
    <% if d.public %> Public <% end %><br /> 
    <% end %> 
</div> 
0

あなたのビューでそれを呼び出します情報。

<div class="collectionList"> 
    <% @user.collections.each do |d| %> 
     <div class="collectionSample"> 
     <strong><%= link_to d.name, {:controller => "collections", :action => "show", :id => d.id } %></strong><br /> 
     Distinct Cards: <%= d.card_collections.count %> <br /> 
     Total Cards: <%= d.card_collections.sum(:card_counts) %> 
     <% if d.public %> Public <% end %><br /> 
     </div> 
    <% end %> 
    </div> 
関連する問題