質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

5724閲覧

rails カウント数を表示

NishidaRyu416_

総合スコア113

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/12/23 04:27

#カウント数を表示するようにしたい
具体的には、あるuserが作ったwordが、favoriteされた数
##model

ruby

1class Word < ActiveRecord::Base 2 3 acts_as_taggable 4 validates :question,presence:true,length: { minimum: 2 } 5 validates :answer,presence:true,length: { minimum: 2 } 6 validates :title ,presence:true,length:{minimum:2} 7 validate :tag_list_tag_validation 8 9 belongs_to :group 10 belongs_to :user 11 has_many :favorites,dependent: :destroy 12 13 def tag_list_tag_validation 14 tag_validation = tag_list 15 tag_validation.split(",") 16 if tag_validation.length < 1 17 errors.add(:tag_list, "タグを入力してください") 18 end 19 if tag_validation.length < 2 20 errors.add(:tag_list, "タグは2個以上入力してください") 21 end 22 if tag_validation.length > 5 23 errors.add(:tag_list, "タグは5個までです") 24 end 25 end 26 27 def favorite?(user) 28 favorites.where(user_id: user.id).exists?# exists?の説明 user.idが一致する人を探す。いたら、true いなかったら false 29 end 30 31 32 33 scope :with_keywords, -> keywords { 34 if keywords.present? 35 columns = [:question,:answer,:title] 36 where(keywords.split(/[[:space:]]/).reject(&:empty?).map {|keyword| 37 columns.map { |a| arel_table[a].matches("%#{keyword}%") }.inject(:or) 38 }.inject(:or)) 39 end 40 } 41end

ruby

1 2class User < ActiveRecord::Base 3 4 before_save { self.email = self.email.downcase } 5 validates :name, presence: true, length: { maximum: 50 } 6 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i 7 validates :email, presence: true, length: { maximum: 255 }, 8 format: { with: VALID_EMAIL_REGEX }, 9 uniqueness: { case_sensitive: false } 10 11 validates :local, absence: true, 12 on: :create 13 validates :local, allow_blank: true, 14 length: { minimum: 2, maximum: 20 }, 15 on: :update 16 17 validates :profile, absence: true, #入力を許さない 18 on: :create 19 validates :profile, allow_blank: true, #空文字のときバリデーションスキップ 20 length: { minimum: 2, maximum: 100 }, 21 on: :update 22 23 has_secure_password 24 25 has_many :words 26 has_many :groups 27 has_many :tests 28 29 has_many :following_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy 30 has_many :following_users, through: :following_relationships, source: :followed 31 has_many :followed_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy 32 has_many :followed_users, through: :followed_relationships, source: :follower 33 34 has_many :favorites, dependent: :destroy 35 has_many :favorite_users, through: :favorites, source: :word 36 37 has_many :tests, dependent: :destroy 38 has_many :tests_users, through: :favorites, source: :word 39 40 has_many :relation_groups,dependent: :destroy 41 has_many :relation_users,through: :relation_groups,source: :word 42 43 # 他のユーザーをフォローする 44 def follow(other_user) 45 following_relationships.find_or_create_by(followed_id: other_user.id) 46 end 47 48 # フォローしているユーザーをアンフォローする 49 def unfollow(other_user) 50 following_relationship = following_relationships.find_by(followed_id: other_user.id) 51 following_relationship.destroy if following_relationship 52 end 53 54 # あるユーザーをフォローしているかどうか? 55 def following?(other_user) 56 following_users.include?(other_user) 57 end 58end

ruby

1 2class Favorite < ActiveRecord::Base 3 belongs_to :user 4 belongs_to :word 5 6 validates :user, presence: true 7 validates :user_id, uniqueness: { scope: :word_id } 8 validates :word, presence: true 9end

##controller

ruby

1 2class UsersController < ApplicationController 3 before_action :logged_in_user,except:[:new,:create,:userwords,:favorites] 4 before_action :set_params, only: [:show, :edit, :update,:favorites,:userwords] 5 before_action :correct_user, only: [:edit, :update ,:favorites] 6 7 def show 8 @val=@user.words.favorites(@user) 9 end 10 11 def new 12 @user = User.new 13 end 14 15 def create 16 @user = User.new(user_params) 17 if @user.save 18 flash[:success] = "Welcome to Share-単!" 19 redirect_to @user 20 else 21 render 'new' 22 end 23 end 24 25 def edit 26 end 27 28 def update 29 if @user.update(user_params ) 30 flash[:success] = "Completion!" 31 redirect_to @user 32 else 33 render 'edit' 34 end 35 end 36 37 def userwords 38 @title = 'user_words' 39 @words = @user.words.order('created_at desc') 40 @user.favorites.each do | favorite | 41 if !@words.include? favorite.word 42 @words << favorite.word 43 end 44 end 45 render 'words' 46 end 47 48 def favorites #favoriteの集合体 49 @title = 'favorite_words' 50 @words = @user.favorite_users.order('created_at desc') 51 render 'words' 52 end 53 54 private 55 def set_params 56 @user = User.find(params[:id]) 57 end 58 59 def user_params 60 params.require(:user).permit(:name, :email, :password, 61 :password_confirmation,:local ,:profile) 62 end 63 64 def correct_user 65 redirect_to root_path if @user != current_user 66 end 67end 68

##view

ruby

1 2<div class="user-profile"> 3 <div class="icon"> 4 <%= gravatar_for user, size: 100 %> 5 </div> 6 <div class="name"> 7 <h1><%= user.name %></h1> 8 </div> 9 <div class="status"> 10 <ul> 11 <li> 12 <div class="status-label">Followings</div> 13 <div class="status-value"> 14 <%= user.following_users.count %> 15 </div> 16 </li> 17 <li> 18 <div class="status-label">Followers</div> 19 <div id="followers" class="status-value"> 20 <%= user.followed_users.count %> 21 </div> 22 </li> 23 <li> 24 <div class="status-label">作成問題数</div> 25 <div id="want_count" class="status-value"> 26 <!--TODO--> 27 <%=user.words.count%> 28 </div> 29 </li> 30 <li> 31 <div class="status-label">お気に入り</div> 32 <div id="have_count" class="status-value"> 33 <!--TODO--> 34 <%=user.favorites.count%> 35 </div> 36 </li> 37 <li> 38 <div class="status-label">お気に入りされた数</div> 39 <div id="have_count" class="status-value"> 40 <!--TODO--> 41 <%=@val%>#ここに表示したい 42 </div> 43 </li> 44 </ul> 45 46 <%= render 'users/follow/follow_form' if logged_in? && @user != current_user %> 47 <br> 48 <%= link_to @user.name + "さんの単語帳",userwords_user_url, class:"btn btn-info"%> 49 </div> 50</div> 51

###回答よろしくお願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Viewで表示する前にコンソールで目的の数字が取得できるか確認したほうが良さそうです。

具体的には、あるuserが作ったwordが、favoriteされた数

↑は、このコードで取得できますか?

ruby

1user = User.first 2word_ids = user.words.map(&:id) 3user.favorites.where(word_id: word_ids).count

投稿2017/01/12 02:53

mingos

総合スコア4025

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

NishidaRyu416_

2017/01/12 15:58

すみません。 解決済みでした。 丁寧にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問