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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1665閲覧

comparison of Symbol with 0 failedというエラーに対応したいです。

ganbarou_nippon

総合スコア18

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/11 06:37

ruby

1 @victry_times=notes_all.where(:profit_and_loss > 0).group("HOUR(CONVERT_TZ(created_at, '+00:00', '+09:00'))").count.values.map{|money| money/notes_all.group("HOUR(CONVERT_TZ(created_at, '+00:00', '+09:00'))").count*100}

というコードに対して

ruby

1comparison of Symbol with 0 failed

というエラーが出ます。
そもそもこのエラーがどういったものかがわからないのですが、どうすれば解決できますでしょうか?

以下はこの問題に関わる全てのコードです。

ruby

1def index 2 @user=current_user 3 @notes=@user.notes.all.order("id DESC").page(params[:page]).per(30) 4 notes_all=@user.notes 5 @comments=[] 6 if notes_all.present? 7 @notes.each do |note| 8 @comments << note.comments.count 9 end 10 @profit_symbol=notes_all.group(:symbol).sum(:profit_and_loss) 11 profit_month=notes_all.group("MONTH(created_at)") 12 @profit_changes = profit_month.sum(:profit_and_loss).values.map{|money| money/notes_all.sum(:profit_and_loss)*100} 13 @profit_times=notes_all.group("HOUR(CONVERT_TZ(created_at, '+00:00', '+09:00'))").sum(:profit_and_loss).values.map{|money| money/notes_all.sum(:profit_and_loss)*100} 14 @victry_times=notes_all.where(:profit_and_loss > 0).group("HOUR(CONVERT_TZ(created_at, '+00:00', '+09:00'))").count.values.map{|money| money/notes_all.group("HOUR(CONVERT_TZ(created_at, '+00:00', '+09:00'))").count*100} 15 @victory_rates= notes_all.where(:profit_and_loss > 0).group("MONTH(created_at)").count.values.map{|money| money/profit_month.count*100} 16 this_month = Date.today.all_month 17 before_month=Date.today.last_month 18 profit_this_month = [] 19 profit_before_month = [] 20 notes_all.each do |note| 21 if this_month.include?(Date.parse(note[:created_at].to_s)) 22 profit_this_month << note 23 elsif before_month.include?(Date.parse(note[:created_at].to_s)) 24 profit_before_month << note 25 end 26 end 27 @profit_this_month= profit_this_month.sum(:profit_and_loss) 28 @profit_this_month_average = @profit_this_month / profit_this_month.length 29 @victory_rate_this_month = profit_this_month.where(:profit_and_loss > 0).count/profit_this_month.count*100 30 @profit_before_month= profit_before_month.sum(:profit_and_loss) 31 @profit_before_month_average= @profit_before_month / profit_before_month.length 32 @victory_rate_before_month=profit_before_month.where(:profit_and_loss > 0).count/profit_before_month.count*100 33 end 34 end

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

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

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

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

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

guest

回答1

0

ベストアンサー

そもそもこのエラーがどういったものかがわからないのですが

エラーメッセージのとおりです。シンボルと0を>で比較しようとしたのでエラーとなっています。

どうすれば解決できますでしょうか?

.where(:profit_and_loss > 0)が問題となっています。とりあえず.where('profit_and_loss > 0')と文字列で渡せば回避できます。

投稿2020/02/11 06:43

maisumakun

総合スコア145184

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

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

ganbarou_nippon

2020/02/11 07:00

教えていただきありがとうございます。 '''@victry_times=notes_all.where('profit_and_loss > 0').group("HOUR(CONVERT_TZ(created_at, '+00:00', '+09:00'))").count.values.map{|money| money/notes_all.group("HOUR(CONVERT_TZ(created_at, '+00:00', '+09:00'))").count*100}''' としたところ '''Hash can't be coerced into Integer'''' というエラーになり、文字列は受け付けないようなのですが、、どうすれば良いでしょうか?
maisumakun

2020/02/11 07:05

それはおそらく、この文字列指定とは関係ないエラーかと思います。
ganbarou_nippon

2020/02/11 07:19

承知しました!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問