Question

I've managed to scope my posts to just the users I'm following with the following code:

following_ids = current_member.following_members.map(&:id)
@statuses = Status.where(member_id: following_ids).order("created_at DESC")

But I want to also include my own posts and I'm having trouble accomplishing this. So basically combining that code with this code:

@statuses = @member.statuses.order('created_at desc').all

What's the best way to do this.

Was it helpful?

Solution 2

I figured this one out I needed to use push not flatten.

@statuses = Status.where("member_id in (?)", following_ids.push(current_member.id)).order("created_at desc").all

OTHER TIPS

Pull both arrays of objects and flatten them.

following_ids = current_member.following_members.map(&:id)  
@statuses = Status.where(member_id: following_ids).order("created_at DESC")
@statuses << @member.statuses.order('created_at desc').all
@statuses = @statuses.flatten

To see the list in one chronological list, call sort_by

so instead of

@status = @statuses.flatten

use

@status = @statuses.flatten.sort_by{ |status| status.created_at }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top