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

41 views
Skip to first unread message

David Williams

unread,
Mar 2, 2016, 1:48:26 PM3/2/16
to rubyonra...@googlegroups.com
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 %>
<h1>No new posts</h1>
<% 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.

--
Posted via http://www.ruby-forum.com/.

Mahcsig

unread,
Mar 2, 2016, 3:56:49 PM3/2/16
to rubyonra...@googlegroups.com
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


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5317cf6d26c8523f7496dc2931f4d1a7%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

David Williams

unread,
Mar 2, 2016, 4:05:07 PM3/2/16
to rubyonra...@googlegroups.com
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 Williams <li...@ruby-forum.com>

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

Mahcsig

unread,
Mar 2, 2016, 4:15:15 PM3/2/16
to rubyonra...@googlegroups.com
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


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Colin Law

unread,
Mar 2, 2016, 4:21:42 PM3/2/16
to Ruby on Rails: Talk
On 2 March 2016 at 18:47, David Williams <li...@ruby-forum.com> wrote:
> 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])

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

>
> 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 %>
> <h1>No new posts</h1>
> <% 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.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5317cf6d26c8523f7496dc2931f4d1a7%40ruby-forum.com.

David Williams

unread,
Mar 2, 2016, 4:36:06 PM3/2/16
to rubyonra...@googlegroups.com
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.

David Williams

unread,
Mar 2, 2016, 4:37:30 PM3/2/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1181847:
> On 2 March 2016 at 18:47, David Williams <li...@ruby-forum.com> 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.

David Williams

unread,
Mar 2, 2016, 4:43:47 PM3/2/16
to rubyonra...@googlegroups.com
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 %>
<h1>No new posts</h1>
<% end %>

Colin Law

unread,
Mar 2, 2016, 4:52:01 PM3/2/16
to Ruby on Rails: Talk
On 2 March 2016 at 21:43, David Williams <li...@ruby-forum.com> 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

> <%= 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 %>
> <h1>No new posts</h1>
> <% end %>
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8a06021c43b603b07ed4c52ff1e36142%40ruby-forum.com.

David Williams

unread,
Mar 2, 2016, 5:07:21 PM3/2/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1181852:
> On 2 March 2016 at 21:43, David Williams <li...@ruby-forum.com> 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.

Colin Law

unread,
Mar 2, 2016, 5:18:07 PM3/2/16
to Ruby on Rails: Talk
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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7519ae9bd5b199cf223e513ad94674b4%40ruby-forum.com.

David Williams

unread,
Mar 2, 2016, 6:07:13 PM3/2/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1181854:
> On 2 March 2016 at 22:06, David Williams <li...@ruby-forum.com> 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.
Reply all
Reply to author
Forward
0 new messages