別のテーブルproductsに記載されているbrand_idに応じてブランド名を取得したい。ブランド ID は「brandids」に配列として保存されます。これが私のコントローラーコードです。
def index
catids = Array.new
brandids = Array.new
@products = nil
if(!(params[:catid].nil?) && params[:catid].to_s.downcase != "all")
catids = arrayConvertion(params[:catid])
end
if(!(params[:brandid].nil?) && params[:brandid].to_s.downcase != "all")
brandids = arrayConvertion(params[:brandid])
end
if(catids.length == 0 && brandids.length == 0)
@products = Product.solr_search do
paginate :page => params[:page], :per_page => 3
end
@brands={}
@products.each do |p|
brand = p.BrandsTest.first(p.brands_test_id)
@brands[p.id] = BrandsTest.name
end
end
このコントローラーのビューは次のとおりです。
<% @products.results.each do |p| %>
<%= image_tag("Images/#{p.id}.jpeg",:alt => p.name) %>
<p> Name: </p>
<%= p.name %>
<p> Price: </p>
<%= p.price %>
<p> Discount: </p>
<%= p.discount %>
<p> Brand Name: </p>
<%= @brands[p.id] %>
<% end %>
製品モデルコードは:
class Product < ActiveRecord::Base
belongs_to :brands_test
belongs_to :categories_test
attr_accessible :id, :name
searchable do
integer :id
text :name
integer :brands_test_id
integer :categories_test_id
end
end
どのクエリを追加する必要があり、どこに追加する必要がありますか? これで私を助けてください。