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?
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
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…
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.
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.
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
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.
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.