2016-11-29 2 views
0

Versuchen Sie herauszufinden, wie Sie eine Menge Logik aus der Sicht in Controller/Modell verschieben können.Verschieben der Funktion von der Ansicht zum Controller Rails 5

(ich glaube, es angenommen hat, in dem Modell gehen?)

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> 

Jeder Benutzer eine Sammlung hat, die jede Kollektion eine card_collection haben kann, die jeweils von denen kann eine Karte haben.

Die Anzahl der 'card_collections' ist die Anzahl der Karten in deinem Deck. Die Summe von "card_collections.card_counts" ist die Gesamtanzahl der Karten. Ich denke, wenn ich es als 'Summe der Karten' und 'Anzahl der Karten' im Modell definiere, kann ich es so nennen, aber ich bin mir nicht sicher, wie.

Ich denke auch, meine Logik wäre einfacher, wenn ich es in das Modell verschieben könnte, da ich pluck/sum verwenden könnte, um die Ergebnisse schneller zu bekommen?

Sammlung

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

    # validates :user_id, presence: true 
end 

Karte

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

können Sie bitte Beziehungen zwischen Tabellen (in has-many-toys-Struktur), show-Methode in Controller und Felder im Modell. –

Antwort

0

Hier fügen Sie einfach folgende c Ode in Ihrem Controller:

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

Ihrer Ansicht Datei

<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> 

Glücklich Coding !!

+0

Ich nahm an, es war Benutzer-Controller, so habe ich Ihre Abfrage aber jetzt 'SQLite3 :: SQLException: keine solche Spalte: collections.card_counts: SELECT *, count (collections.id) als card_collections_count, Summe (collections.card_counts) als card_counts FROM "Benutzer" INNER JOIN "Sammlungen" ON "Sammlungen". "User_id" = "Benutzer". "ID" ' Als Antwort. Ich habe auch versucht, @users = User.joins (: collections,: card_collections) .select ("*, count (card_collections.id) als card_collections_count, sum (card_collections.card_counts) als card_counts") und es Fehler beim Beitritt . – DNorthrup

+0

Fügen Sie hier Ihre Fügefehlerprotokolle ein – user100693

0

Sie können es auf Ihrem CardCollection Modell als

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 

es dann in der Ansicht rufen

<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

ich in der Lage war, herauszufinden, wie die Sum Funktion verwenden, um die nutzen Information.

<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> 
Verwandte Themen