这是我的search_data
方法:
def search_data
{
email: email,
skills: interests.pluck(:name)
}
end
我的User
has_many :skills
。当我运行User.where(conditions).reindex
时,它会加载skills
记录,例如
SELECT * FROM skills WHERE skills.user_id = 1
SELECT * FROM skills WHERE skills.user_id = 2
SELECT * FROM skills WHERE skills.user_id = 3
SELECT * FROM skills WHERE skills.user_id = 4
. . .
我想知道是否有可能让它像这样加载:
SELECT * FROM skills WHERE skills.user_id IN (1, 2, 3, 4, . . .)
答案 0 :(得分:2)
您可以在建立索引时使用预先加载,因此要重新索引User
模型,您需要编写User.includes(:skills).reindex
然后您也需要在map
代替pluck
:
def search_data
{
email: email,
skills: interests.map(&:name)
}
end