我有一个表格:
<%= 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 配合使用。