Rails 4-合并多个find_by请求

时间:2022-08-27 00:10:15

I have a model called "Post" which has a user defined guid integer field and also a name field which is being used as a blog. What I needed to do was be able to find the post with the url of the post by adding either the guid or name params, but not together, and if the guid gets requested, it redirects to the name param of the post. I have a route called blog which I'm using as a path instead of post. (Ex: http://mysite/blog/53 redirects to http://mysite/blog/name-given-to-the-post)

我有一个名为“Post”的模型,它有一个用户定义的guid整数字段,还有一个名字字段,用作博客。我需要做的是能够通过添加guid或名称参数来找到帖子的帖子,但不能一起添加,如果guid被请求,它会重定向到帖子的名称参数。我有一条名为博客的路线,我用它作为路径而不是帖子。 (例如:http:// mysite / blog / 53重定向到http:// mysite / blog / name-given-to-the-post)

I managed to get it working, but I know it's very inefficient because it's making two near identical find requests, but I know of no other way to do it.

我设法让它工作,但我知道这是非常低效的,因为它正在发出两个几乎相同的查找请求,但我知道没有其他方法可以做到这一点。

Post_controller.rb

def show
     @post = Post.find_by guid: params[:guid]
     @slug = Post.find_by_slug(params[:guid])
     if request.path != blog_path(@slug)
       redirect_to blog_path(@post)
     end
  end

I mapped blog to post in my routes which is why you see blog_path for example:

我将博客映射到我的路线中,这就是为什么你看到blog_path的例子:

get 'blog/:guid', to: 'posts#show', as: 'blog'
  resources :posts, path: 'blog', as: 'blog'

Post.rb (Model):

class Post < ActiveRecord::Base

  before_create :create_slug

def to_param 
  slug
end

def create_slug
  self.slug = self.name.parameterize
end
.....
end

I also had to change @post to @slug in my show.html.erb file because of the backwards type of logic I unfortunately used.

我还必须在我的show.html.erb文件中将@post更改为@slug,因为我不幸使用了向后类型的逻辑。

<%= @slug.name %>
<%= sanitize(@slug.summary) %>

1 个解决方案

#1


1  

I don't understand all the questions in your post, especially the part about the @post and @slug stuff...

我不明白你帖子中的所有问题,尤其是关于@post和@slug内容的部分......

Why are you not assigning both variables to the same object? It should be the same object no matter if it's fetched from the database via the slug or the name, or am i mistaken?

为什么不将两个变量分配给同一个对象?它应该是同一个对象,无论它是通过slug或名称从数据库中获取的,还是我错了?

You can query the objects using an OR query in your sql like Post.where('slug = ? OR name = ?', params[:guid], params[:guid])

您可以在sql中使用OR查询查询对象,如Post.where('slug =?OR name =?',params [:guid],params [:guid])

#1


1  

I don't understand all the questions in your post, especially the part about the @post and @slug stuff...

我不明白你帖子中的所有问题,尤其是关于@post和@slug内容的部分......

Why are you not assigning both variables to the same object? It should be the same object no matter if it's fetched from the database via the slug or the name, or am i mistaken?

为什么不将两个变量分配给同一个对象?它应该是同一个对象,无论它是通过slug或名称从数据库中获取的,还是我错了?

You can query the objects using an OR query in your sql like Post.where('slug = ? OR name = ?', params[:guid], params[:guid])

您可以在sql中使用OR查询查询对象,如Post.where('slug =?OR name =?',params [:guid],params [:guid])