如何动态链接Rails中的where子句? [英] How do I dynamically chain where clauses in rails?

查看:90
本文介绍了如何动态链接Rails中的where子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频模型和一个标签模型.标签HABTM视频和视频HABTM标签.

I have a Videos model and a Tags model. Tags HABTM Videos and Videos HABTM Tags.

class Tag < ApplicationRecord
  has_and_belongs_to_many :videos
  scope :with_tag, -> (id) { where(id: id) }
end

class Video < ApplicationRecord
  has_and_belongs_to_many :tags
end

我想知道如何创建一个查询,其中来自用户的标签数量未知.因此,我可以说:向我展示所有标有(123 AND 124 AND 125)的视频".最终目标是我不知道用户将传递多少标签,因此我需要知道如何创建范围链,最终能够将整个内容传递到ransack上以进行文本搜索结果视频的某些字段.我该怎么做?

I want to know how to create a query where an unknown number of Tags would come in from the user. So I can say, "show me all the videos that are tagged with (123 AND 124 AND 125)". The ultimate goal being that I don't know how many tags the user is going to pass in, so I need to know how to create a chain of scopes and ultimately be able to pass the entire thing into ransack to do a text search on some fields of the resulting videos. How can I accomplish this?

更新:我想知道如何用Rails和ActiveRecord做到这一点.我知道如何使用SQL完成此操作.但是,仅通过SQL来完成它不会使我将结果关系传递给ransack.

UPDATE: I want to know how to do this with Rails and ActiveRecord. I know how I can accomplish this with SQL. But accomplishing it only with SQL will not allow me to pass the resulting relation onto ransack.

推荐答案

我创建了一些相关的表来编写此答案,并针对表中的数据测试查询.所以,我在这里获取帖子,它们的标签都为1和2.

I have created some related tables to write this answer, and test the query against the data the table has. So, I am here fetching the posts, which has tag 1 and 2 both.

rails-test_development=# select id from posts;
 id
----
  1
  2
  3
(3 rows)

rails-test_development=# select id from tags;
 id
----
  2
  3
  4
  5
  6
(5 rows)

rails-test_development=# select post_id, tag_id from posts_tags;
 post_id | tag_id
---------+--------
       1 |      2
       2 |      3
       2 |      2
       2 |      1
       3 |      1
       3 |      2
       1 |      4
(7 rows)

rails-test_development=# WITH posts_tags_cte AS (
rails-test_development(#         SELECT post_id, array_agg(tag_id) as tags
rails-test_development(#         FROM posts_tags
rails-test_development(#         WHERE tag_id in (1, 2)
rails-test_development(#         GROUP BY post_id
rails-test_development(# )
rails-test_development-# SELECT posts.id FROM posts_tags_cte JOIN posts ON posts.id = posts_tags_cte.post_id
rails-test_development-# WHERE posts_tags_cte.tags @> array[1, 2]::int8[];
 id
----
  3
  2
(2 rows)

现在加入Rails:

rails-test$ rails c
Running via Spring preloader in process 7361
Loading development environment (Rails 5.2.1)
2.5.1 :001 > Post.by_tag([1,2]).first.attributes
  Post Load (1.7ms)  SELECT  "posts".* FROM     (WITH posts_tags_cte AS (
      SELECT post_id, array_agg(tag_id) as tags
      FROM posts_tags
      WHERE tag_id in (1,2)
      GROUP BY post_id
    )
    SELECT posts.* FROM posts_tags_cte JOIN posts ON posts.id = posts_tags_cte.post_id
    WHERE posts_tags_cte.tags @> array[1,2]::int8[]) as posts
 ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => {"id"=>2, "name"=>"Postppp", "title"=>"Post", "content"=>"I don't know", "created_at"=>Sun, 09 Sep 2018 09:48:33 UTC +00:00, "updated_at"=>Sun, 09 Sep 2018 09:48:33 UTC +00:00, "month_and_year"=>Sun, 09 Sep 2018}
2.5.1 :002 > Post.by_tag([1,2]).last.attributes
  Post Load (1.3ms)  SELECT  "posts".* FROM     (WITH posts_tags_cte AS (
      SELECT post_id, array_agg(tag_id) as tags
      FROM posts_tags
      WHERE tag_id in (1,2)
      GROUP BY post_id
    )
    SELECT posts.* FROM posts_tags_cte JOIN posts ON posts.id = posts_tags_cte.post_id
    WHERE posts_tags_cte.tags @> array[1,2]::int8[]) as posts
 ORDER BY "posts"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => {"id"=>3, "name"=>"Post A", "title"=>"Post title", "content"=>"Lorem Ipsum is simply dummy text of the printing and typesetting industry.", "created_at"=>Sun, 09 Sep 2018 09:52:57 UTC +00:00, "updated_at"=>Sun, 09 Sep 2018 09:52:57 UTC +00:00, "month_and_year"=>Sun, 09 Sep 2018}
2.5.1 :003 >

我将范围定义为:

class Post < ApplicationRecord
  has_and_belongs_to_many :tags

  scope :by_tag, -> (tag_ids) {

    sql = sanitize_sql_array [<<-SQL, tag_ids, tag_ids]
    (WITH posts_tags_cte AS (
      SELECT post_id, array_agg(tag_id) as tags
      FROM posts_tags
      WHERE tag_id in (?)
      GROUP BY post_id
    )
    SELECT posts.* FROM posts_tags_cte JOIN posts ON posts.id = posts_tags_cte.post_id
    WHERE posts_tags_cte.tags @> array[?]::int8[]) as posts
    SQL

    from(sql)
  }
end

我从此答案获得了帮助.

这篇关于如何动态链接Rails中的where子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆