【问题标题】:Rails SQL query in controller method控制器方法中的 Rails SQL 查询
【发布时间】: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=&gt;params[:page], :per_page=&gt;3 end end。并将视图文件更改为&lt;% @products.results.each do |p| %&gt; &lt;% @brands.results.each do |b| %&gt; &lt;p&gt; Brand name: &lt;/p&gt; &lt;%= b.name %&gt; 但我收到以下错误。没有为名称为“name”的 BrandsTest 配置字段

标签: sql ruby-on-rails solr


【解决方案1】:
@products = Product.solr_search do
    with :brands_test_id, brandids[0..brandids.length]
    paginate :page=>params[:page], :per_page=>3
end
@brands={}
@products.each do |p|
brand=Brand.find_by_id(p.brand_id)
@brands[p.id]=brand.name
end

观看次数:

<% @products.results.each do |p| %>
    #other code
   <p> Brand: </p>
   <%= @brands[p.id]   
<% end %>

我不确定我是否理解你想要做什么。

【讨论】:

  • 我收到了这个错误。 ProductsController#index 中的 Sunspot::UnrecognizedFieldError 没有为名称为“name”的产品配置字段。问题是产品表中没有品牌名称。所以我需要使用产品表中的brand_id从品牌表中提取品牌名称。这就是为什么我会收到我认为的错误。 @乔
  • 您的意思是我的代码出现此错误吗?实际上我的代码是直接从 Brand 表中获取品牌名称,而不仅仅是通过产品 id 的键来散列,这样您就可以轻松地在产品视图中获得价值。
  • 品牌属于产品?通过 has_one?尝试品牌 = p.brand
  • 能否请您显示模型代码和产品创建方法?
  • 是的,品牌与产品之间存在一对多的关系。我也尝试了 p.brands.first 但得到了相同的错误。Brands 有 2 列 id 和 name。 Products 表有 5 列。即 id、name、brands_test_id、discount 和 price。目前我正在使用brands_test_id显示产品名称、折扣、价格。现在,我想使用该brands_test_id 显示品牌名称。请帮我解决这个问题。
【解决方案2】:

我根据你给出的答案对我的应用进行了一些修改。这是控制器部分。

if(catids.length == 0 && brandids.length == 0)
    @products = Product.solr_search do
        paginate :page=>params[:page], :per_page=>3
    end
    @brands={}
    @products.results.each do |p|
        brand=BrandsTest.find_by_id(p.brands_test_id)
        @brands[p.id]=brand.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 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多