当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - using ransack to traverse a complex associated tag hierarchy

问题描述:

I have a model I which to search using Ransack called Question and it has the associated models question_tags through question_taggings:

has_many :question_taggings, :dependent => :destroy

has_many :question_tags, :through => :question_taggings

I can do a basic search using these tags with something like:

Question.ransack(question_tags_id_or_question_tags_parent_id_in: [447,449]).result

But for tags, we're are using the closure_tree gem which presents a problem of how to search a more complicated hierarchy of tags.

Specifically I'd like to be able to search parent tags and be able to return all of the tags associated with the tag and its descendants. Which I can do using closure_tree like so (with 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"

Eg tag 447 is sports and it has the following 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 |

+-----+-------------------+---------------------------+---------------------------+-----------+

My question is how could I integrate this search using Ransack? First off, would I create a custom ransacker? The problem is they don't give any complicated examples or even examples which use associated models. ;-(

Any advice would be great! Thanks,

网友答案:

Is it acceptable to split into two queries?

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: