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

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

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

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby

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

Ruby on Rails 6

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

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

0回答

358閲覧

既に同じ自転車が別のラインで持ち出されている場合という条件式を記述したい

hhhhhhggggg

総合スコア2

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby

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

Ruby on Rails 6

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

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2023/01/17 03:05

編集2023/01/17 11:35

前提

Docker環境上でRuby on railsを用いて、自転車のレンタル機能を開発しております。
持ち出し登録画面(bicycle_logs/take)、返却画面(bicycle_logs/return)、自転車使用履歴画面(bicycle_logs)がある。

db/schema.rb

ruby

1create_table "bicycle_logs", comment: "自転車使用履歴", force: :cascade do |t| 2 t.bigint "bicycle_id", comment: "自転車id" 3  t.bigint "line_id", comment: "ラインid" 4 t.datetime "take_datetime", comment: "持ち出し日時" 5 t.bigint "take_user_id", comment: "持ち出し日時" 6 t.datetime "return_datetime", comment: "返却日時" 7 t.bigint "return_user_id", comment: "返却日時" 8 t.integer "before_weight", comment: "使用前重量" 9 t.integer "after_weight", comment: "使用後重量" 10 t.integer "used_weight", comment: "使用量" 11 t.datetime "created_at", precision: 6, null: false 12 t.datetime "updated_at", precision: 6, null: false 13 t.index ["bicycle_id"], name: "index_paintcan_logs_on_paintcan_id" 14 end 15 16create_table "bicycles", comment: "自転車", force: :cascade do |t| 17 t.datetime "arrival_datetime", comment: "入荷日時" 18 t.string "product_number", comment: "品番" 19 t.integer "weight", comment: "重量" 20 t.bigint "location_id", comment: "ロケーションid" 21 t.bigint "create_user_id", comment: "作成者id" 22 t.bigint "update_user_id", comment: "更新者id" 23 t.datetime "created_at", precision: 6, null: false 24 t.datetime "updated_at", precision: 6, null: false 25 end

実現したいこと

持ち出し登録画面で持ち出し登録した際に、既に同じ自転車が別のラインで持ち出されている場合は、自動で返却処理がなされるようにしたい
そのために自転車使用履歴から既に同じ自転車が別のラインで持ち出されている場合という条件式を記述したい

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

既に同じ自転車が別のラインで持ち出されていることを検知できない

該当のソースコード

app/controllers/bicycle_logs/takes_controller.rb

ruby

1def create 2 bicycle_logs = [] 3 4 ActiveRecord::Base.transaction do 5 6 bicycle_logs_params.each do |bicycle_log_params| 7 bicycle_log = BicycleLog.new(bicycle_log_params) 8 bicycle_log.take_datetime = params[:take_datetime] || Time.current 9 bicycle_log.take_user_id = params[:take_user_id] 10 bicycle_log.line_id = params[:line_id] 11 bicycle_log.created_by(current_user) 12 end 13 14 raise ActiveRecord::Rollback unless success 15 end 16 17 end

app/views/bicycle_logs/takes/show.html.slim

ruby

1//入力フォーム 2つのVue.jsで定義 2#app(v-cloak) 3 4 = form_with(url: bicycle_logs_take_path) do |f| 5 bicycle-logs-form( 6 v-model='bicycleLogs' 7 now=Time.current.iso8601[0..18] 8 :current-user='currentUser' 9 :last-line='line' 10 :production-schedule='productionSchedule' 11 ) 12 = f.submit //持ち出し登録ボタン ここをクリックしたときの処理を追加したい 13 14= javascript_packs_with_chunks_tag 'bicycle_logs/takes/show'

app/javascript/components/bicycle_logs/takes/Form.vue

javascript

1<template lang='pug'> 2div 3 .d-flex.justify-content-between 4 .form-group 5 .form-inline 6 .form-group 7 label ライン 8 LineSelect( 9 name='line_id' 10 v-model='lineId' 11 :line.sync='line' 12 required 13 ) 14 .form-group 15 label 持ち出し者 16 UserSelect( 17 name='take_user_id' 18 v-model='takeUserId' 19 :user.sync='takeUser' 20 required 21 ) 22 div 23 button.btn.btn-info(type='button' @click='add(null)') 追加 24 25 .table-sticky.max-vh-60(ref='recordsContainer') 26 table.table.table-form.table-striped.table-bordered 27 thead 28 tr 29 th RFID 30 th 品番 31 th ロット番号 32 th 重量(g) 33 th 製品 34 th 35 tbody 36 template(v-for='(record, index) in records') 37 BicycleLogRow( 38 ref='detailRows' 39 :name='`bicycle_logs[${index}]`' 40 :key='record.key' 41 :record='records[index]' 42 :date='datetime' 43 @remove='remove' 44 @insert='insert(index)' 45 ) 46</template> 47 48<script> 49import {v4 as uuid} from 'uuid' 50import BicycleLogRow from './BicycleLogRow' 51import LineSelect from '~/components/LineSelect' 52import UserSelect from '~/components/UserSelect' 53 54const newRecord = () => { 55 return { 56 key: uuid(), 57 beforeWeight: null, 58 product: null, 59 paintNumber: null, 60 } 61} 62 63export default { 64 components: { 65 bicycleLogRow, 66 }, 67 model: { 68 prop: 'records', 69 }, 70 props: { 71 records: { 72 type: Array, 73 }, 74 now: { 75 type: String, 76 }, 77 currentUser: { 78 type: Object, 79 }, 80 lastLine: { 81 type: Object, 82 }, 83 productionSchedule: { 84 type: Object 85 }, 86 }, 87 data(){ 88 return { 89 datetime: this.now, 90 lineId: this.lastLine?.id, 91 line: this.lastLine, 92 takeUserId: this.currentUser.id, 93 takeUser: this.currentUser, 94 bPaint: this.production.bPaint, 95 tPaint: this.production.tPaint, 96 Number: this.production.Number, 97 } 98 }, 99 async mounted(){ 100 this.records.forEach((record) => { 101 record.key = uuid() 102 }) 103 if(this.records.length === 0 ){ 104 if(this.bPaint == null && this.tPaint == null){ 105 this.add(null) 106 } 107 else{ 108 if (this.bPaint != null){ 109 this.add(this.bPaint) 110 } 111 if (this.tPaint != null){ 112 this.add(this.tPaint) 113 } 114 } 115 } 116 117 await this.$nextTick() 118 this.$refs.detailRows[0].focus() 119 }, 120 methods: { 121 async add(paint){ 122 let newRecort = newRecord() 123 newRecort = {product: this.Number, paint: paint} 124 this.records.push(newRecort) 125 await this.$nextTick() 126 scrollToBottom(this.$refs.recordsContainer) 127 }, 128 async insert(index){ 129 arrayInsert(this.records, newRecord(), index + 1) 130 await this.$nextTick() 131 this.$refs.detailRows.at(-1).focus() 132 }, 133 remove(record){ 134 arrayDelete(this.records, record) 135 if(this.records.length === 0){ 136 this.add(null) 137 } 138 }, 139 }, 140} 141</script>

app/javascript/components/bicycle_logs/takes/BicycleLogRow.vue

javascript

1<template lang="pug"> 2tr(@keypress.stop='onKeypress') 3 td 4 input.form-control( 5 ref='defaultFocusInput' 6 type='text' 7 v-model='record.rfid' 8 @change='onRfidChange' 9 required 10 ) 11 input( 12 type='hidden' 13 v-model='bicycle.id' 14 :name='`${name}[bicycle_id]`' 15 ) 16 td {{bicycle.Number}} 17 td {{bicycle.lotNumber}} 18 td 19 input.form-control( 20 type='number' 21 v-model.number='record.beforeWeight' 22 :name='`${name}[before_weight]`' 23 step='any' 24 required 25 ) 26 td 27 TextField.w-100( 28 v-model='record.product' 29 :name='`${name}[product]`' 30 :autocomplete='products' 31 @focus='onProductFocus' 32 ) 33 td.text-nowrap 34 button.btn.btn-warning(type='button' @click='remove(record)') 削除 35</template> 36 37<script> 38import axios from "axios"; 39import insertCommand from "~/lib/event/insertCommand"; 40 41function checkProductNumber(currentPaintNumber, loadedPaintNumber) { 42 if (currentPaintNumber == null || currentPaintNumber === "") { 43 return true; 44 } else { 45 if (currentPaintNumber !== loadedPaintNumber) { 46 const ask = window.confirm( 47 `品番が一致しません。` 48 ); 49 return ask; 50 } else { 51 return true; 52 } 53 } 54} 55 56export default { 57 model: { 58 prop: "record" 59 }, 60 props: { 61 name: { 62 type: String 63 }, 64 record: { 65 type: Object 66 }, 67 date: { 68 type: String 69 }, 70 currentRfId: { 71 type: Number 72 } 73 }, 74 data() { 75 return { 76 bicycle: {}, 77 products: [] 78 }; 79 }, 80 async mounted() { 81 this.bicycle = { Number: this.record.paint, lotNumber: "" }; 82 }, 83 methods: { 84 focus() { 85 this.$refs.defaultFocusInput.focus(); 86 }, 87 onKeypress(e) { 88 if (insertCommand(e)) { 89 this.$emit("insert"); 90 } 91 }, 92 remove(record) { 93 this.$emit("remove", record); 94 }, 95 async onRfidChange(e) { 96 const input = e.target; 97 const id = this.record.rfid; 98 try { 99 const result = await axios.get(`/api/web/bicycles/${id}`); 100 console.log(result.data); 101 if ( 102 checkNumber( 103 this.bicycle.Number, 104 result.data.Number 105 ) 106 ) { 107 this.bicycle = result.data; 108 input.setCustomValidity(""); 109 this.currentRfId = id; 110 } else { 111 this.record.rfid = this.currentRfId; 112 } 113 } catch (e) { 114 this.bicycle = {}; 115 input.setCustomValidity(""); 116 } 117 }, 118 async onProductFocus(e) { 119 this.products = []; 120 121 const params = { 122 date: this.date 123 }; 124 const result = await axios.get("/api/web/productions", { 125 params 126 }); 127 console.log(result); 128 129 this.products = result.data.map( 130 it => `${it.productNumber} ${it.productName}` 131 ); 132 } 133 } 134}; 135</script>

試したこと

持ち出し登録した際に、既に同じものが別のラインで持ち出されている場合であることを検知するために以下の記述をしたが、登録しようとしているものと同じ bicycle_id をまず、検知できていないため、何か気づくことがあれば教えて頂けると幸いです。
app/controllers/bicycle_logs/takes_controller.rb

ruby

1def create 2省略 3 bicycle_ids = BicycleLog.find(&:bicycle_id) 4 take_datetimes = BicycleLog.find(&:take_datetime) 5 return_datetimes = BicycleLog.find(&:return_datetime) 6 line_ids = BicycleLog.find(&:line_id) 7 8 if bicycle_ids.present? 9 logger.debug("登録しようとしているものと同じbicycle_idがある") 10 if take_datetimes.present? && return_datetimes.nil? 11 logger.debug("持ち出し日時のみあって、返却日時がないデータは存在する") 12 if line_ids.present? 13 logger.debug("登録しようとしているラインと異なる") 14 logger.debug("返却処理を行い、再度登録する") 15 end 16 end 17 else 18 logger.debug("通常通り登録する") 19 end 20end

DBの状態(一つのデータ抜粋)

select * from bicycle_logs;の結果

id | bicycle_id | line_id | take_datetime | take_user_id | return_datetime | return_user_id | before_weight | after_weight | used_weight | created_at | updated_at

1| 2 | 3 | 2022-06-11 16:57:74 | 2 | 2022-06-11 17:41:80 | 1 | 594 | 415 | 169 | 2022-12-17 17:51:09.96 | 2022-12-17 17:51:09.96

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

バージョン情報
Rails 6.1.4.7
ruby 3.1.2
Docker 20.10.11

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

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

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

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

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

yuma.inaura

2023/01/17 03:26

持ち出されてるというのはDBのデータ的にはどんな状態なんでしょう?
hhhhhhggggg

2023/01/17 05:12

ご回答ありがとうございます! DBの方を質問の方に追加しました!
yuma.inaura

2023/01/17 09:15

>登録しようとしているものと同じpaintcan_idをまず、検知できていないため paintcan_idって何なんでしょうか
hhhhhhggggg

2023/01/17 11:36

すみません、 bicycle_id の記述ミスでした。
hhhhhhggggg

2023/01/17 11:38

bicyclesテーブルの自転車の製品を登録しているテーブルのidを外部キーによって自転車使用履歴のテーブルに持ってきているカラムとなります。自転車の製品情報です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問