简体   繁体   中英

How to use where method to find all content id in array rails

In the code below, "@search.sensors" is an array that has been searched. The sensors belong to a publication. Is there way to find Publication of each sensor in the array?

Publication Model

class Publication < ApplicationRecord
    has_many :sensors, dependent: :destroy, autosave: true
end

Sensor Model

class Sensor < ApplicationRecord
    belongs_to :publication
end

I tried this but it does not work

Search Model

@sensors = @search.sensors
@publications = Publication.joins(:sensors).where(id: params[@search.sensors.map(&:id)])
@publications.uniq

如果@search.sensors解析为Sensor类型的数组,那么您可以轻松地执行以下操作:

Publication.where(id: @search.sensors.map(&:publication_id))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM