Ruby on rails 计数搜索中返回的记录-ROR
我试图让Rails返回搜索中返回的记录计数(搜索结果已找到);我可以让Rails使用 盘点 然而,当我输入@advsearch.count时,它就爆炸了。有什么想法吗?下面的代码是我的控制器:Ruby on rails 计数搜索中返回的记录-ROR,ruby-on-rails,search,controller,Ruby On Rails,Search,Controller,我试图让Rails返回搜索中返回的记录计数(搜索结果已找到);我可以让Rails使用 盘点 然而,当我输入@advsearch.count时,它就爆炸了。有什么想法吗?下面的代码是我的控制器: def new @advsearch = Advsearch.new end def create @advsearch = Advsearch.new(params[:advsearch]) if @advsearch.save flash[:notic
def new
@advsearch = Advsearch.new
end
def create
@advsearch = Advsearch.new(params[:advsearch])
if @advsearch.save
flash[:notice] = "Mr. Roboto dug through #{(Inventory.count)} records"
redirect_to @advsearch
else
render :action => 'new'
end
end
其次是我的模型:
class Advsearch < ActiveRecord::Base
def inventories
@inventories ||= find_inventories
end
private
def find_inventories
Inventory.find(:all, :conditions => conditions)
end
def keyword_conditions
["inventories.item LIKE ?", "%#{keywords}%"] unless keywords.blank?
end
def vender_conditions
["inventories.vender_id = ?", vender_id] unless vender_id.blank?
end
def area_conditions
["inventories.area_id = ?", area_id] unless area_id.blank?
end
def chem_conditions
["inventories.chem_id = ?", chem_id] unless chem_id.blank?
end
def location_conditions
["inventories.location_id = ?", location_id] unless location_id.blank?
end
def instock_conditions
["inventories.instock = ?", instock] unless instock.blank?
end
def conditions
[conditions_clauses.join(' AND '), *conditions_options]
end
def conditions_clauses
conditions_parts.map { |condition| condition.first }
end
def conditions_options
conditions_parts.map { |condition| condition[1..-1] }.flatten
end
def conditions_parts
private_methods(false).grep(/_conditions$/).map { |m| send(m) }.compact
end
end
class-Advsearchconditions)
结束
def关键字_条件
[“inventory.item LIKE?”,“%#{keywords}%”除非keywords.blank?
结束
def供应商_条件
[“inventory.vender\u id=?”,vender\u id]除非vender\u id.blank?
结束
def区域_条件
[“inventory.area\u id=?”,area\u id]除非area\u id.blank?
结束
def化学条件
[“inventory.chem_id=?”,chem_id]除非chem_id.blank?
结束
def位置_条件
[“inventory.location\u id=?”,location\u id]除非location\u id.blank?
结束
def instock_条件
[“inventory.instock=?”,instock]除非instock.blank?
结束
def条件
[条件从句.连接('AND'),*条件选项]
结束
def条款
条件_parts.map{|条件|条件.first}
结束
def条件\u选项
条件_parts.map{|条件|条件[1..-1]}.展平
结束
def条件和部件
private_methods(false).grep(/_conditions$/).map{m|send(m)}.compact
结束
结束
应该让你数一数
应该给你一个计数。你把“供应商”拼错了,仅供参考。你能粘贴你收到的错误信息吗?另外,您在哪里使用@advsearch.count?1。NoMethodError未定义的方法计数2。我在这个上下文中使用它:flash[:notice]=“搜索通过#{(Inventory.count)}记录并找到#{@advsearch.count}”您拼写“vendor”错误,仅供参考。您能粘贴您收到的错误消息吗?另外,您在哪里使用@advsearch.count?1。NoMethodError未定义的方法计数2。我在这个上下文中使用它:flash[:notice]=“搜索通过#{(Inventory.count)}记录并找到#{@advsearch.count}”
def create
@advsearch = Advsearch.new(params[:advsearch])
if @advsearch.save
flash[:notice] = "Mr. Roboto dug through #{(@advsearch.inventories.length)} records"
redirect_to @advsearch
else
render :action => 'new'
end
end