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

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

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

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

Q&A

解決済

1回答

636閲覧

時間管理アプリにて、同日の同時間の選択ができないようにしたい

toshi.n

総合スコア0

Ruby

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

0グッド

0クリップ

投稿2021/08/11 08:11

前提・実現したいこと

プログラミング学習1ヶ月の初学者です。
現在、オリジナルアプリで時間管理の簡易アプリを作成しています。
その中で、時間の選択、同日で同時間の選択をできないよう制限を加えたいと思っています。
時間選択にはactive_hashを利用しています。

制限を加えるということで、modelにて条件つきのバリデーションを記述できないかと思い、
サイトを確認し、コードを色々と書きましたが、記述の仕方等がわからず、実現できませんでした。

お時間ある方がいらっしゃれば、すみませんが、ご教授のほど、よろしくお願いいたします。

発生している問題・エラーメッセージ

現状、同日の同時刻を選択できて、保存ができる。

該当のソースコード

Ruby

ソースコード ・model time_content class TimeContent < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :time_step belongs_to :content class << self def content_data @time_contents = TimeContent.all content_graph_data = {} @time_contents.each_with_index do |time_content, index| content_graph_data = { time_content.content.name => 0 } if index.zero? if content_graph_data.key?(time_content.content.name) content_graph_data[time_content.content.name] += 1 else content_graph_data[time_content.content.name] = 1 end end content_graph_data end def background_color_content_data @time_contents = TimeContent.all content_graph_data = {} background_colors = [] @time_contents.each_with_index do |time_content, index| if index.zero? content_graph_data = { time_content.content.name => 0 } background_colors << time_content.content.backcolor end if content_graph_data.key?(time_content.content.name) content_graph_data[time_content.content.name] += 1 else content_graph_data[time_content.content.name] = 1 background_colors << time_content.content.backcolor end end background_colors end end with_options numericality: { other_than: 1, message: "can't be blank" } do validates :time_step_id validates :content_id end with_options presence: true do validates :detail validates :start_time end belongs_to :user end ・model time_step class TimeStep < ActiveHash::Base self.data = [ { id: 1, name: '--' }, { id: 2, name: '9:00~9:30' }, { id: 3, name: '9:30~10:00' }, { id: 4, name: '10:00~10:30' }, { id: 5, name: '10:30~11:00' }, { id: 6, name: '11:00~11:30' }, { id: 7, name: '11:30~12:00' }, { id: 8, name: '12:00~12:30' }, { id: 9, name: '12:30~13:00' }, { id: 10, name: '13:00~13:30' }, { id: 11, name: '13:30~14:00' }, { id: 12, name: '14:00~14:30' }, { id: 13, name: '14:30~15:00' }, { id: 14, name: '15:00~15:30' }, { id: 15, name: '15:30~16:00' }, { id: 16, name: '16:00~16:30' }, { id: 17, name: '16:30~17:00' }, { id: 18, name: '17:00~17:30' } ] include ActiveHash::Associations has_many :time_contents end ・contoroller class TimeContentController < ApplicationController before_action :authenticate_user!, except: [:index] before_action :set_time_content, only: [:show, :edit, :update, :destroy] before_action :move_into_index, only: [:show, :edit] def index @time_contents = TimeContent.where(user_id: current_user) # @time_contents = TimeContent.all #1つのカレンダーでユーザー全員の内容を閲覧 @day = Time.zone.today @content_month_graph_data = TimeContent.where(user_id: current_user).content_data @background_colors = TimeContent.where(user_id: current_user).background_color_content_data # @content_week_graph_data = TimeContent.where(user_id: current_user).content_data @content_day_graph_data = TimeContent.where(user_id: current_user, start_time: @day).content_data @background_colors = TimeContent.where(user_id: current_user, start_time: @day).background_color_content_data end def new @time_content = TimeContent.new end def create @time_content = TimeContent.new(time_content_params) if @time_content.save redirect_to root_path else render :new end end def show end def edit end def update if @time_content.update(time_content_params) redirect_to time_content_path else render :edit end end def destroy redirect_to root_path if @time_content.destroy end private def time_content_params params.require(:time_content).permit(:start_time, :time_step_id, :content_id, :detail).merge(user_id: current_user.id) end def set_time_content @time_content = TimeContent.find(params[:id]) end def move_into_index redirect_to action: :index if current_user.id != @time_content.user.id end end

試したこと

with_options numericality: { other_than: 1, message: "can't be blank" } do
validates :time_step_id, uniqueness: true , if: Proc.new { |a| a.start_time.present? && user_id.present? && time_step_id.present? }
validates :content_id

特に、表示は変わりませんでした。

補足情報(FW/ツールのバージョンなど)

ruby :'2.6.5'
rails :'> 6.0.0'
カレンダー:'simple_calendar', '
> 2.0'
グラフ  :'chartkick'

ここにより詳細な情報を記載してください。

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

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

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

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

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

maisumakun

2021/08/11 08:13

バリデーションによる制限は、基本的に「サーバサイド」でしか働かない(ブラウザで入力するのを阻止するのではなく、一回フォームを送信されてから判定してエラーにする)ですが、それで問題ないでしょうか?
toshi.n

2021/08/11 09:36

ご返信ありがとうございます。 思いついたのが、バリデーションでの制限だけでした。 ブラウザ側での制限方法もあるんですね。 もし、ブラウザ側の方法もあるのでしたら、今後のためにヒントだけでもいただければ、幸いです。
guest

回答1

0

自己解決

"scope" を利用することで解決することができました。
models/time_content
validates :time_step_id, uniqueness: { scope: [:time_step_id, :start_time] }
ありがとうございました。

投稿2021/08/13 05:30

toshi.n

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問