Rails迭代一个集合并返回它



我想在rails集合上做一个映射,但我需要结果是一个活动记录集合而不是数组。目前我正在做以下操作,但这返回一个数组。

MyModel.all.map do |model|
model.tap do |m|
response = fetcher.new.call(m.name)
m.rating = response[:rating]
end
end
# 1. get all the records 
all_records = MyModel.all
# 2. Iterate over the collection and modify each model as you are doing
all_records.each do |model|
response = fetcher.new.call(model.name)
model.rating = response[:rating]
end
# 3. Return the modified collection
all_records

最新更新