How do I add my own posts plus following users post with Acts_As_Follower Rails 4

I’ve been trying to combine my own post with the users that I follow in
chronological order DESC. Can you help me build the proper query method
for it?

What I currently have is the code below

def feed
following_ids = current_user.following_users.map(&:id)
@following_activities =
Post.where(user_id:following_ids).order(“created_at
desc”).paginate(page:params[:page])
@following_activities << current_user.posts.order(‘created_at
desc’).all
@following_activities = @following_activities.flatten.sort_by
{|post| post.created_at }
end

Feed.html.erb
<% if @following_activities.any? %>
<% @following_activities.each do |user| %>
<%= link_to(image_tag(user.avatar.url(:thumb), style:
‘text-decoration: none;’, class: ‘round-image-50-trendy-warrior’),
user_path(user)) %>
<%= user.post.username %>
<%= user.post.body %>
<%= image_tag(user.post.photo.url(:medium), style: ‘’) %>
<% end %>
<% else %>

No new posts


<% end %>

The approach that I’m using is not causing any server errors. But, I’m
not seeing the post that I’ve already created, they aren’t being loaded.

How about just adding the current_user.id to the following_ids array?
You
could then pull all the posts in one shot, and remove the next two
lines.

~Marc

~Mahcsig

On Wed, Mar 2, 2016 at 10:47 AM, David W. [email protected]

Mahcsig wrote in post #1181844:

How about just adding the current_user.id to the following_ids array?
You
could then pull all the posts in one shot, and remove the next two
lines.

~Marc

~Mahcsig

On Wed, Mar 2, 2016 at 10:47 AM, David W. [email protected]

Can you write-out your approach to simplify/refactor the code above???

def feed
following_ids = current_user.following_users.map(&:id)
following_ids << current_user.id
@following_activities =
Post.where(user_id:following_ids).order(“created_at
desc”).paginate(page:params[:page])
end

~Mahcsig

On 2 March 2016 at 18:47, David W. [email protected] wrote:

Post.where(user_id:following_ids).order(“created_at
desc”).paginate(page:params[:page])

I don’t think you want paginate here, and since you are going to sort
it again later there is no point sorting here either.

@following_activities << current_user.posts.order('created_at

desc’).all

You want + here not << as you just want to concatenate the arrays,
then you will not need to flatten in the next line. Also no need to
sort in line above

@following_activities = @following_activities.flatten.sort_by

{|post| post.created_at }
end

As I said above, no need for the flatten

Also, assuming that you have User has_many following_users and User
has_many posts then you should be able to say something like
User has_many following_posts through following_users …
You will need a bit more on the end get it to work, not exactly sure
what you need there. Perhaps someone else will know exactly what you
need. Then you will be able to say
current_user.following_posts
to get all those posts.

So you will be able to say
@following_activities = (current_user.posts +
current_user.following_posts).sort…

Colin

Mahcsig wrote in post #1181846:

def feed
following_ids = current_user.following_users.map(&:id)
following_ids << current_user.id
@following_activities =
Post.where(user_id:following_ids).order(“created_at
desc”).paginate(page:params[:page])
end

~Mahcsig

I’m getting an error for the user’s avatar image. For whatever reason,
it’s not seeing the full user model with attributes included. I’m using
Carrierwave btw.

<%= link_to(image_tag(user.avatar.url(:thumb), style: ‘text-decoration:
none;’, class: ‘round-image-50’), user_path(user)) %>

undefined method `avatar’ for #Post:0xe2e9138

But, I see that it is loading the information within the instance
variables.

Colin L. wrote in post #1181847:

On 2 March 2016 at 18:47, David W. [email protected] wrote:

Post.where(user_id:following_ids).order(“created_at
desc”).paginate(page:params[:page])

Also, assuming that you have User has_many following_users and User
has_many posts then you should be able to say something like
User has_many following_posts through following_users …
You will need a bit more on the end get it to work, not exactly sure
what you need there. Perhaps someone else will know exactly what you
need. Then you will be able to say
current_user.following_posts
to get all those posts.

So you will be able to say
@following_activities = (current_user.posts +
current_user.following_posts).sort…

Colin

Thanks for responding guys. Right now, I’m using the methods that are
included with the Acts_As_Follower Gem.

Iterating over the @following_activities instance variable is giving me
errors for user.avatar.url and user.post. I don’t understand why it’s
not collecting all of the information from the user object.

<% if @following_activities.any? %>
<% @following_activities.each do |user| %>
<%= link_to(image_tag(user.avatar.url(:thumb), style:
‘text-decoration: none;’, class: ‘round-image-50-trendy-warrior’),
user_path(user)) %>
<%= user.post.username %>
<%= user.post.body %>
<%= image_tag(user.post.photo.url(:medium), style: ‘’) %>
<% end %>
<% else %>

No new posts


<% end %>

On 2 March 2016 at 21:43, David W. [email protected] wrote:

Iterating over the @following_activities instance variable is giving me
errors for user.avatar.url and user.post. I don’t understand why it’s
not collecting all of the information from the user object.

<% if @following_activities.any? %>
<% @following_activities.each do |user| %>

In the code you posted earlier the activities are Post objects not
User objects. Possibly you want
@following_activities.each do |post|
and then post.user.photo… etc

Colin

Colin L. wrote in post #1181852:

On 2 March 2016 at 21:43, David W. [email protected] wrote:

Iterating over the @following_activities instance variable is giving me
errors for user.avatar.url and user.post. I don’t understand why it’s
not collecting all of the information from the user object.

<% if @following_activities.any? %>
<% @following_activities.each do |user| %>

In the code you posted earlier the activities are Post objects not
User objects. Possibly you want
@following_activities.each do |post|
and then post.user.photo… etc

Colin

It’s working now. Thank you and Mahcsig for helping me.

On 2 March 2016 at 22:06, David W. [email protected] wrote:

User objects. Possibly you want
@following_activities.each do |post|
and then post.user.photo… etc

Colin

It’s working now. Thank you and Mahcsig for helping me.

Just to point out that the fundamental mistake you made here was
calling it @following_activities. If you had called it
@following_posts or something similar then you would probably fnot
have made the error.
For the future simple debugging can be achieved by inserting logger
output in the code at appropriate points, so for example if after the
line
<% @following_activities.each do |user| %>
you insert the line
logger.info “user = #{user.inspect}”
then the debug would be inserted into log/development.log which would
probably have enabled you to see the problem.

Colin

Colin L. wrote in post #1181854:

On 2 March 2016 at 22:06, David W. [email protected] wrote:

Just to point out that the fundamental mistake you made here was
calling it @following_activities. If you had called it
@following_posts or something similar then you would probably fnot
have made the error.
For the future simple debugging can be achieved by inserting logger
output in the code at appropriate points, so for example if after the
line
<% @following_activities.each do |user| %>
you insert the line
logger.info “user = #{user.inspect}”
then the debug would be inserted into log/development.log which would
probably have enabled you to see the problem.

Colin

Yes, it was a fatal flaw in my naming convention that led to the
confusion. Thank you.