0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

配列をactiverecordへ変換する

Last updated at Posted at 2023-01-28

はじめに

find_in_batchesメソッドはactiverecordに対してのみ使用できるので配列をactiverecordへ変換したい

方法

配列に格納されたデータのidのみを別の配列に格納する
(push・・・配列に要素を追加)

@channel_ids = []

channels = user.channels
channels.each do |channel|
  @channel_ids.push(channel.id)
end

idのみを格納した配列をwhere句へ指定して、再度データを取得することでactiverecordへ変換したデータが取得できる

target_channels = Channel.where(id: @channel_ids)

参考文献

もっと簡単に書けました
wwhere(id: user.channels.pluck(:id))

0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?