我有一个数组Pictures。每张图片has_many评论。
如果我有一组图片@pictures,如何从所有图片中获取具有特定属性的所有评论@pictures?以下代码是否有一个不错的 Ruby 单行代码?:
@comments = []
@pictures.each do |pic|
pic.comments.each do |comment|
if comment.text == "test"
@comments << comment
end
end
end
注意:我知道我可能可以从数据库查询的一行中得到这个,但我认为使用我已经拥有的数据会更有效,而不是重新查询数据库中的所有图片,当我只关心我已经拥有的图片的某个子集。