I have a problem with ordering search results on rails. I want to order the posts after I searched or filtered but what I m doing doesn t seem to work. Here is my code.
员额:控制员。
display
@posts = Post.where("user_id IN (?)", current_user.following_ids)
@posts = @posts.find(Like.group(:post_id).pluck(:post_id)).order(params[:change]) if params[:change].present?
end
I have a posts_controller which displays the posts user are following to, it works fine. However, when I added an extra code trying to order the posts by the number of likes, error occurs. It seems that the second line in post controller extracts all the posts with like instead of only the posts the user are following. If I replace @posts.find with Post.find, all the posts result will be shown and ordered by the number of likes.
具体来说,这句话就是说,命令(重新编号: :desc)是罚款。 它只命令员额用户。
类似.rb
class Like < ApplicationRecord
belongs_to :post, foreign_key: "post_id"
belongs_to :user
validates_uniqueness_of :post_id, scope: :user_id
end
如:控制器。
class LikesController < ApplicationController
def create
@like = current_user.likes.create(post_id: params[:post_id])
redirect_back(fallback_location: root_path)
end
def destroy
@like = Like.find_by(post_id: params[:post_id], user_id: current_user.id)
@like.destroy
redirect_back(fallback_location: root_path)
end
end
如果我问一问一问,在方案拟订中我就是一个非常新的问题,请原谅我。 非常感谢你。
错误信息如下: