views:

91

answers:

1

I'm trying to get Rails to return a count of the records returned in a search (search results found); I can get Rails to count the total amount of records using

Inventory.count

However when I enter @advsearch.count it blows up. Any ideas? The code below is my controller:

  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

followed by my model:

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
+3  A: 
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

Should get you a count.

Tony Fontenot