What I’d like to be able to do is obtain the posts for a given Group. But I only want to be able to obtain the last post (most recent) for each user in that group.
For example, I want to be able to do something like: Group.find(6).recent_posts
and have it return an array that would equal
posts = [ ]
Group.find(6).users.each do |user|
posts << user.posts.last
end
Posted are the models in my app
class Group < ActiveRecord::Base
attr_accessible :name, :share_link
has_many :memberships
has_many :users, :through => :memberships
validates :name, presence: :true
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
attr_accessible :group_id, :user_id, :admin
end
class Post < ActiveRecord::Base
belongs_to :user
attr_accessible :title, :url, :user_id
validates :title, presence: :true
end
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid
has_many :memberships
has_many :groups, :through => :memberships
has_many :posts
end