Ransack存储在db中的搜索字符串数组

时间:2015-07-06 23:27:01

标签: ruby-on-rails arrays search ransack

我试图使用搜索搜索文章。

在我关于如何根据标签获取文章的一个问题中,我发现了如何做到这一点:

Find articles where array

现在我只想用搜索进行搜索但是我收到错误

我喜欢这个

<%= search_form_for @q do |f| %>
        <div class="row">
          <div class="col-md-2">
            <%= f.label :tags_in %> <span data-toggle='tooltip', data-placement='right', title='Una o dos palabras máximo para expecificar lo que está buscando' class="glyphicon glyphicon-question-sign" ></span>
            <%= f.search_field :tags_in, class: 'form-control' %>
          </div>
          <div class="col-md-2">
            <%= f.submit 'Buscar', class: 'btn btn-primary m-t-large' %>
          </div>
        </div>

      <% end %>

执行xbox搜索时出现的错误如下:

PG::InvalidTextRepresentation: ERROR:  array value must start with "{" or dimension information
LINE 1: ...es".* FROM "articles" WHERE "articles"."tags" IN ('xbox')  O...

array error ransack

继承人:

def index
    @q = Article.ransack(params[:q])
    @articles = @q.result.paginate(page: params[:page], :per_page => 10).order('created_at DESC')
  end

如何使用标签搜索文章?

谢谢!

1 个答案:

答案 0 :(得分:1)

this issue所述,您需要将gem postgres_ext添加到您的项目中:

# config/initializers/ransack.rb
Ransack.configure do |config|
  %w[
    contained_within
    contained_within_or_equals
    contains
    contains_or_equals
    overlap
  ].each do |p|
    config.add_predicate p, arel_predicate: p, wants_array: true
  end
end

并将其添加到初始化程序中:

contains

之后,您将能够在序列化数组中使用module MyAppHelper def list_logos(clss) logos = Dir.glob("engines/myapp/app/assets/images/myapp/logos/*.{gif,png,jpg}") list_items = logos.map do |logo| content_tag("li", class: clss) do image_tag logo.gsub("engines/myapp/app/assets/images/", "") end end list_items.join end end