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

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

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

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

Q&A

0回答

584閲覧

Rails 取得したデータの整形

usa_mal

総合スコア16

Ruby on Rails

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

0グッド

0クリップ

投稿2020/04/22 15:28

編集2020/04/23 00:41

Rails で質問させてください。
異なる2つのテーブル(モデル)を union で結合して取得した下記取得データの before のようなデータを下記取得データのafterような形に整形して取得することは可能でしょうか?

やりたいこととしては、
・"type" が "1" のデータは、"child_id", "child_name", "child_period", "child_note", "child_done", "child_order_number" を削除してしまう。
・"type" が "2" のデータは、"id" が同じものでまとめて、"child_id", "child_name", "child_period", "child_note", "child_done", "child_order_number" の6項目については、"project_task_todos" の中に、"child_order_number" の昇順にまとめてしまう。

調べてみたのですが、思うような情報を探すことができませんでした。
やり方をご教授いただけますと幸いです。

##model

rb

1class Project < ApplicationRecord 2 3 4 has_many :todos, dependent: :destroy 5 accepts_nested_attributes_for :todos, allow_destroy: true 6 7 has_many :tasks, dependent: :destroy 8 accepts_nested_attributes_for :tasks 9 10 def self.getCombineTodos(project_id) 11 sql = <<-EOS 12 (select '1' as type_flg, pt.pt_id as id, pt.name as name, pt.period, pt.note, pt.done, pt.order_number, 0 as child_id, 0 as child_name, 0 as child_period, 0 as child_note, 0 as child_done, 0 as child_order_number from (select project_todos.id + 100000000 as pt_id, project_id, period, project_todos.note, done, project_todos.name as name, order_number from project_todos where project_todos.project_id = :project_id) as pt) 13 union 14 (select '2' as type_flg, pt.id + 200000000, pt.name, pt.period, pt.note, pt.done, pt.order_number, ptt.id as child_id, ptt.name as child_name, ptt.period, ptt.note as chile_note, ptt.done as child_done, ptt.order_number as child_order_number from project_tasks as pt 15 left outer join project_task_todos as ptt on pt.id = ptt.project_task_id where pt.project_id = :project_id) ORDER BY order_number ASC 16 EOS 17 find_by_sql([sql, { project_id: project_id }]) 18 end 19 20end

##controller
上記のmodel内のsqlで取得できるデータを、下記のメソッドで、フロント(Vue)側に渡します。

# todoとtaskをまとめて同列にして取得 def combine_todos project = Project.getCombineTodos(params[:id]) render json: { status: 'SUCCESS', message: '', data: project } end

##取得データ

before

1 "data":[ 2 { 3 "id": 100000001, 4 "name": "td1", 5 "type": "1", 6 "period": "2020-06-20", 7 "note": "sample", 8 "done": 1, 9 "order_number": 1, 10 "child_id": 0, 11 "child_name": "0", 12 "child_period": "0", 13 "child_note": "0", 14 "child_done": 0, 15 "child_order_number": 0 16 }, 17 { 18 "id": 200000001, 19 "name": "task1", 20 "type": "2", 21 "period": "2020-06-20", 22 "note": "task1", 23 "done": 0, 24 "order_number": 2, 25 "child_id": 1, 26 "child_name": "ta1", 27 "child_period": null, 28 "child_note": "1-1", 29 "child_done": 0, 30 "child_order_number": 1 31 }, 32 { 33 "id": 200000001, 34 "name": "task1", 35 "type": "2", 36 "period": "2020-06-20", 37 "note": "task1", 38 "done": 0, 39 "order_number": 2, 40 "child_id": 2, 41 "child_name": "ta2", 42 "child_period": null, 43 "child_note": "1-2", 44 "child_done": 0, 45 "child_order_number": 2 46 }, 47 { 48 "id": 100000002, 49 "name": "td2", 50 "type": "1", 51 "period": "2020-06-20", 52 "note": null, 53 "done": 0, 54 "order_number": 3, 55 "child_id": 0, 56 "child_name": "0", 57 "child_period": "0", 58 "child_note": "0", 59 "child_done": 0, 60 "child_order_number": 0 61 }, 62 { 63 "id": 200000002, 64 "name": "task2", 65 "type": "2", 66 "period": "2020-06-20", 67 "note": "task2", 68 "done": 0, 69 "order_number": 4, 70 "child_id": 4, 71 "child_name": "ta2", 72 "child_period": null, 73 "child_note": "2-2", 74 "child_done": 0, 75 "child_order_number": 2 76 }, 77 { 78 "id": 200000002, 79 "name": "task2", 80 "type": "2", 81 "period": "2020-06-20", 82 "note": "task2", 83 "done": 0, 84 "order_number": 4, 85 "child_id": 3, 86 "child_name": "ta1", 87 "child_period": null, 88 "child_note": "2-1", 89 "child_done": 0, 90 "child_order_number": 1 91 } 92 ] 93

after

1 "data":[ 2 { 3 "id": 100000001, 4 "name": "td1", 5 "type": "1", 6 "period": "2020-06-20", 7 "note": "sample", 8 "done": 1, 9 "order_number": 1, 10 }, 11 { 12 "id": 200000001, 13 "name": "task1", 14 "type": "2", 15 "period": "2020-06-20", 16 "note": "task1", 17 "done": 0, 18 "order_number": 2, 19 "project_task_todos": [ 20 { 21 "child_id": 1, 22 "child_name": "ta1", 23 "child_period": "2020-06-20", 24 "child_note": "1-1", 25 "child_done": 0, 26 "child_order_number": 1 27 }, 28 { 29 "child_id": 2, 30 "child_name": "ta2", 31 "child_period": "2020-06-20", 32 "child_note": "1-2", 33 "child_done": 0, 34 "child_order_number": 2 35 } 36 ] 37 }, 38 { 39 "id": 100000002, 40 "name": "td2", 41 "type": "1", 42 "period": "2020-06-20", 43 "note": "note_test, 44 "done": 0, 45 "order_number": 3, 46 }, 47 { 48 "id": 200000002, 49 "name": "task2", 50 "type": "2", 51 "period": "2020-06-20", 52 "note": "task2", 53 "done": 0, 54 "order_number": 4, 55 "project_task_todos": [ 56 { 57               "child_id": 3, 58 "child_name": "ta1", 59 "child_period": "2020-06-20", 60 "child_note": "2-1", 61 "child_done": 0, 62 "child_order_number": 1 63 }, 64 { 65 "child_id": 4, 66 "child_name": "ta2", 67 "child_period": "2020-06-20", 68 "child_note": "2-2", 69 "child_done": 0, 70 "child_order_number": 2 71 }, 72 ] 73 } 74 ]

####捕捉
ruby-2.6.2
Ruby on Rails 5.2.3

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問