English 中文(简体)
铁路订单搜索结果
原标题:Rails order search results

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

如果我问一问一问,在方案拟订中我就是一个非常新的问题,请原谅我。 非常感谢你。

错误信息如下:

“entergraph

问题回答

请注意,find,在有阵容时,要求阵列中的所有助手都在场或出现错误。 在documentation

If one or more records can not be found for the requested ids, then RecordNotFound will be raised.

我将利用不同的机制检索记录:

@posts = @posts.where(id: Like.group(:post_id).pluck(:post_id)).
              order(params[:change]) if params[:change].present?

这将仅仅确定符合条件的记录。

这感到 little。 能否取得这一结果,取决于您的模型在<条码>/Like<>/code>、<条码>、Post和<条码>之间可能存在何种关系。

I placed an order for a dress on April 1 via Face Book and have not received a confirmation Email yet I have been charged. The dress has not arrived. Can you please advise of next steps.’’Thank you

Debbie Purvis 9175743119





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签