ActiveRecord: keep duplicated objects in .where query
问题 I have an array of ids: ids = [1, 2, 3, 1] If I run: Product.where(id: ids) I will only get one occurence of the Product having the iD 1 . However, I'd like to get as many occurences of the object as there are in the array. How can I do this ? 回答1: You could load the unique records and then transform the result into an Array with multiple references to each book: ids = [2109, 2511, 2108, 2109] tmp_books = Book.find(ids) new_books = ids.map {|x| tmp_books.detect {|b| b.id == x } } That