0
0

More than 1 year has passed since last update.

where メソッドに配列を渡すとインスタンスを配列で返してくれる

Posted at

開発環境

ruby 2.6.5
Ruby on Rails 5.2.5

本題

where メソッドは each とか使わなくても
配列を入れたら
中の要素をすべて処理してくれる

@searching_task_id = Combination.where(label_id: params[:task][:label_name].to_i).pluck(:task_id)
@irb(#<TasksController:0x00007fc0d76f9380>):002:0> @searching_task_id
=> [2333, 2334, 2335]

上のように配列の入った変数を利用して
データベースから
レコードを引っ張るためには

@tasks = Task.where(id: @searching_task_id)
@tasks
=> #<ActiveRecord::Relation [#<Task id: 2333, title: "1", content: "あ", created_at: "2021-06-11 03:56:56", updated_at: "2021-06-11 03:56:56", deadline: nil, status: "", priority: "高", user_id: 68>, #<Task id: 2334, title: "2", content: "あ", created_at: "2021-06-11 03:57:05", updated_at: "2021-06-11 03:57:05", deadline: nil, status: "", priority: "高", user_id: 68>, #<Task id: 2335, title: "3", content: "あ", created_at: "2021-06-11 03:57:17", updated_at: "2021-06-11 03:57:17", deadline: nil, status: "", priority: "高", user_id: 68>]>

そのまま入れるだけで大丈夫

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0