【发布时间】:2013-04-12 06:45:07
【问题描述】:
我想根据另一个表 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
我应该添加什么查询以及我必须在哪里添加? 请帮我解决这个问题。
【问题讨论】:
-
我试过这个。
@products = Product.solr_search do @brands = BrandsTest.solr_search do with :name, brandids[0..brandids.length] paginate :page=>params[:page], :per_page=>3 end end。并将视图文件更改为<% @products.results.each do |p| %> <% @brands.results.each do |b| %> <p> Brand name: </p> <%= b.name %>但我收到以下错误。没有为名称为“name”的 BrandsTest 配置字段
标签: sql ruby-on-rails solr