2010-01-03 21 views
0

我試圖讓Rails返回在搜索中返回的記錄的計數(找到搜索結果);我可以使用Rails的在搜索中返回的計數記錄 - ROR

Inventory.count

然而,當我輸入@ advsearch.count它炸燬來計算記錄總量。有任何想法嗎?下面的代碼是我的控制器:

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 
+2

您拼寫「供應商」錯誤,供參考。 – 2010-01-03 19:08:17

+0

你可以粘貼你得到的錯誤信息嗎?你還在哪裏使用@ advsearch.count? – khelll 2010-01-03 19:11:58

+0

1. NoMethodError未定義的方法計數 2.我在此上下文中使用它:flash [:notice] =「搜索通過#{(Inventory.count)}挖掘記錄並找到#{@advsearch.count}」 – 2010-01-03 19:20:04

回答

4
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 

應該讓你的計數。