本文介绍了Ransack在搜索中是否支持与MetaSearch相同的多态多变的aresly_to关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从MetaSearch gem迁移到Ransack gem,以升级到Rails 3.1,但是在搜索多态关联时遇到问题.现有的MetaSearch语法不适用于Ransack,但是我找不到任何提及任何语法更改的文档.还是Ransack是否支持此功能.

I am migrating from the MetaSearch gem to the Ransack gem for an upgrade to Rails 3.1 and I am having problems with my searches for polymorphic associations. The existing MetaSearch syntax isn't working for Ransack, but I couldn't find any documentation mentioning any syntax changes. Or whether this feature is supported in Ransack.

例如,在MetaSearch github页面上,提供以下类:

For example, from the MetaSearch github page, given the following classes:

class Article < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class Post < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
  validates_presence_of :body
end

您可以像这样在表单中创建一个搜索字段(这显然是从Searchlogic借来的惯例):

you can create a search field in your form like this (which is apparently a convention borrowed from Searchlogic):

<%= f.text_field :commentable_article_type_body_contains %>

我正在使用这种语法,这种语法在MetaSearch中可以很好地工作,但是使用Ransack时,当查询参数包含此字段时,我的应用程序将引发异常.例外是:

I am using this type of syntax, which works perfectly in MetaSearch, but with Ransack my application is throwing an exception when the query parameter contains this field. The exception is:

ActiveRecord::EagerLoadPolymorphicError (Can not eagerly load the polymorphic association :ownable)

有人知道如何在Ransack中进行这种搜索吗?

Does anyone know how to do this type of search in Ransack?

推荐答案

我正在为同一个问题苦苦挣扎(尽管我的错误有所不同).我认为您的代码必须是:

I was struggling with the same problem (although my error was different). I think your code needs to be:

<%= f.text_field :commentable_of_Article_type_body_contains %>

注意大写字母A

对我有用.您可以在此处(这是页面上的最后一个文件)

That worked for me. You can checkout Ernie's tests for polymorphic associations here (it's the last file on the page)

这篇关于Ransack在搜索中是否支持与MetaSearch相同的多态多变的aresly_to关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 08:53