0
我正在尝试获取可追踪项目和试图获取这些用户的帖子。下面是我使用NoMethodError:未定义的方法`评论'为#<Post :: ActiveRecord_Relation:0x007fbaee1f8f00>
def dashboard
@posts = []
@Bposts = []
@users = Follow.where('followable_type == ?',"User").where('follower_id == ?',current_user.id)
@blogs = Follow.where('followable_type == ?',"Blog").where('follower_id == ?',current_user.id)
@users.each do |user|
@posts << Post.where('user_id == ?',user.followable_id).where('blog_id == ?',nil)
end
@blogs.each do |blog|
@Bposts << Post.where('blog_id == ?',nil)
end
if @posts.count != 0
@posts.each do |post|
@comment = post.comments.build
end
end
if @Bposts.count != 0
@Bposts.each do |post|
@comment = post.comments.build
end
end
end
.first将只返回用户的第一个帖子,但我试图撤回用户 – Mr94
是的所有帖子,但是你做到这一点的方式,可能是这样。如果你想这样做,你需要在'@posts.each do | post |'循环里面另外一个循环,因为这样每个帖子都会成为一个'ActiveRecord_Relation'对象,这个对象也需要循环。 –
有没有简单的方法来实现这个功能? – Mr94