我有一个模型,我可以使用名为 Question
的 Ransack 进行搜索,它具有通过 question_taggings
关联的模型 question_tags
:
has_many :question_taggings, :dependent => :destroy
has_many :question_tags, :through => :question_taggings
我可以使用这些标签进行基本搜索,例如:
Question.ransack(question_tags_id_or_question_tags_parent_id_in: [447,449]).result
但对于标签,我们使用的是 closure_tree gem 提出了如何搜索更复杂的标签层次结构的问题。
具体来说,我希望能够搜索父标签并能够返回与该标签及其后代关联的所有标签。我可以像这样使用 closure_tree(使用 SQL):
QuestionTag.find(447).self_and_descendants.to_sql
=> "SELECT \"question_tags\".* FROM \"question_tags\" INNER JOIN \"question_tag_hierarchies\" ON \"question_tags\".\"id\" = \"question_tag_hierarchies\".\"descendant_id\" WHERE \"question_tag_hierarchies\".\"ancestor_id\" = 447 ORDER BY \"question_tag_hierarchies\".generations asc"
例如,标签 447 是 sports
,它具有以下 self_and_descendants:
+-----+-------------------+---------------------------+---------------------------+-----------+
| id | name | created_at | updated_at | parent_id |
+-----+-------------------+---------------------------+---------------------------+-----------+
| 447 | sports | 2014-10-24 15:58:15 +0100 | 2015-01-12 15:10:12 +0000 | |
| 462 | hockey | 2014-10-24 18:21:18 +0100 | 2014-10-24 18:21:18 +0100 | 447 |
| 464 | basketball | 2014-10-24 18:21:31 +0100 | 2014-10-24 18:21:31 +0100 | 447 |
| 465 | olympics | 2014-10-24 18:21:40 +0100 | 2014-10-24 18:21:40 +0100 | 447 |
| 466 | world cup | 2014-10-24 18:21:56 +0100 | 2014-10-24 18:21:56 +0100 | 447 |
| 467 | martial arts | 2014-10-24 18:22:20 +0100 | 2014-10-24 18:22:20 +0100 | 447 |
| 541 | american football | 2014-11-19 06:20:16 +0000 | 2014-11-19 06:20:16 +0000 | 447 |
| 542 | baseball | 2014-11-19 06:20:23 +0000 | 2014-11-19 06:20:23 +0000 | 447 |
| 506 | cricket | 2014-11-06 06:49:05 +0000 | 2014-11-06 06:49:05 +0000 | 447 |
| 661 | winter sports | 2015-02-13 10:46:30 +0000 | 2015-02-13 10:46:30 +0000 | 447 |
| 659 | surfing | 2015-02-13 10:44:05 +0000 | 2015-02-13 10:44:05 +0000 | 447 |
| 660 | horse racing | 2015-02-13 10:46:07 +0000 | 2015-02-13 10:46:07 +0000 | 447 |
| 461 | soccer | 2014-10-24 18:21:12 +0100 | 2014-10-24 18:21:12 +0100 | 447 |
| 463 | football | 2014-10-24 18:21:25 +0100 | 2015-02-13 10:51:28 +0000 | 447 |
| 570 | golf | 2015-01-15 17:22:35 +0000 | 2015-01-15 17:22:35 +0000 | 447 |
| 561 | tennis | 2015-01-15 04:23:41 +0000 | 2015-01-15 04:23:41 +0000 | 447 |
| 601 | motor racing | 2015-02-05 16:29:11 +0000 | 2015-02-05 16:29:11 +0000 | 447 |
| 605 | rugby | 2015-02-05 16:30:32 +0000 | 2015-02-05 16:30:32 +0000 | 447 |
| 621 | snooker | 2015-02-06 14:27:35 +0000 | 2015-02-06 14:27:35 +0000 | 447 |
| 655 | extreme sport | 2015-02-13 10:38:04 +0000 | 2015-02-13 10:38:04 +0000 | 447 |
| 656 | sports equipment | 2015-02-13 10:41:42 +0000 | 2015-02-13 10:41:46 +0000 | 447 |
| 657 | athletics | 2015-02-13 10:42:26 +0000 | 2015-02-13 10:42:32 +0000 | 447 |
| 658 | ballgames | 2015-02-13 10:42:58 +0000 | 2015-02-13 10:42:58 +0000 | 447 |
| 468 | boxing | 2014-10-24 18:22:24 +0100 | 2014-10-24 18:22:24 +0100 | 467 |
| 602 | f1 | 2015-02-05 16:29:16 +0000 | 2015-02-05 16:29:16 +0000 | 601 |
| 603 | motorbikes | 2015-02-05 16:29:24 +0000 | 2015-02-05 16:29:24 +0000 | 601 |
+-----+-------------------+---------------------------+---------------------------+-----------+
我的问题是如何使用 Ransack 集成此搜索?首先,我会创建一个 custom ransacker 吗? ?问题是他们没有给出任何复杂的例子,甚至没有给出使用关联模型的例子。 ;-(
任何建议都会很棒!谢谢,
最佳答案
拆分成两个查询是否可以接受?
tag_ids = QuestionTag
.where(id: [447, 499])
.map{ |t| t.self_and_descendants }
.uniq
.map(&:id)
Question.ransack(question_tags_id_in: tag_ids).result
关于ruby-on-rails - 使用 ransack 遍历复杂的关联标签层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28719122/