I am using R开发者_运维知识库uby on Rails 3 and I would like to improve the following code.
accounts = []
ids.each { |id|
accounts << Account.find_by_id(id)
}
puts accounts
How can I do?
If ids
might have ids which don't correspond to real Account
s:
accounts = ids.map { |id| Account.find_by_id(id) }
Or if you know that all the ids
will be present, you can simplify this to:
accounts = Account.find(ids)
ids.collect {|id| Account.find_by_id(id)}
精彩评论