我想根据另一个表 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
我应该添加什么查询以及我必须在哪里添加?请帮我解决一下这个。