比方說,我有兩個型號帖子和類別:「分裂」的ActiveRecord收集
class Post < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :posts
end
有沒有,讓我做這樣的事情
posts = Post.find(:all)
p = Array.new
p[1] = posts.with_category_id(1)
p[2] = posts.with_category_id(2)
p[3] = posts.with_category_id(3)
...
or
p = posts.split_by_category_ids(1,2,3)
=> [posts_with_category_id_1,
posts_with_category_id_2,
posts_with_category_id_3]
換句話說,「分裂的方法'通過選定的類別ID將所有帖子收集到數組中
'group_by'解決方案正是我所期待的,謝謝! – Vincent 2010-07-16 17:33:15
很好的答案謝謝 – ALFmachine 2017-01-21 03:24:12