Rails,Ransack,HABTM - 多次选择时清空结果

时间:2013-01-15 13:07:55

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我有一张表格:

<%= f.collection_select :carburants_id_eq_all, Carburant.order(:name), :id, :name, {}, { multiple: true } %>

当我只选择1个Carburant时,它会正常渲染我的结果,但如果我选择超过1个Carburant,则结果为空。

这是'index.html.erb'中的表单:

<%= search_form_for @search do |f| %>

  <%= f.label :title_cont, "Name contains" %>
  <%= f.text_field :title_cont %>

  <%= f.collection_select :carburants_id_eq_all, Carburant.order(:name), :id, :name, {}, { multiple: true } %>

  <%= f.submit "Search" %>
<% end %>

我的'post_controller.rb'中的索引操作:

def index
  @search = Post.search(params[:q])
  @posts = @search.result(distinct: true)
end

我的post.rb,carburant.rb和category.rb模型:

class Category < ActiveRecord::Base
  attr_accessible :carburant_id, :post_id
  belongs_to :carburant
  belongs_to :post
end

class Post < ActiveRecord::Base
  attr_accessible :content, :title, :carburant_ids
  has_many :categories
  has_many :carburants, through: :categories
end

class Carburant < ActiveRecord::Base
  attr_accessible :name
  has_many :categories
  has_many :posts, through: :categories
end

我的数据库shema:

ActiveRecord::Schema.define(:version => 20130114004613) do
     create_table "carburants", :force => true do |t|
        t.string   "name",       :limit => nil
        t.datetime "created_at"
        t.datetime "updated_at"
     end

    create_table "categories", :force => true do |t|
      t.integer  "post_id"
      t.integer  "carburant_id"
      t.datetime "created_at",   :null => false
      t.datetime "updated_at",   :null => false
    end

    create_table "posts", :force => true do |t|
     t.string   "title"
     t.text     "content"
     t.datetime "created_at"
     t.datetime "updated_at"
    end

   create_table "recherches", :force => true do |t|
    t.string   "title"
    t.integer  "carburant_id"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
   end
end

先谢谢你。

编辑1:

我尝试在我的表单中替换:carburants_id_eq_all:carburants_id_eq_any只是为了测试,它完全使用EQ_ANY谓词,但仍然没有使用EQ_ALL。

0 个答案:

没有答案