私は次のフォームを持っています:
<%= f.collection_select :carburants_id_eq_all, Carburant.order(:name), :id, :name, {}, { multiple: true } %>
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
私のDBシーマ:
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 ではまだ機能しません。