0

I'm having issues showing out all of my drinks in my index.html.haml. I have recently moved to start using thinking sphinx for searching after watching Ryan Bates' (thinking sphinx railscast. As part of the move to sphinx I changed @drinks = Drink.all to @drinks = Drink.search(params[:search]) and now its not showing the drink names on my index page

Drink Model

class Drink < ActiveRecord::Base

  attr_accessible :name, :detail, :recipe_steps_attributes

  has_many :recipe_steps, :dependent => :destroy
  has_many :ingredients, through: :recipe_steps
  has_one :glass

  validates_uniqueness_of :name, case_sensitive: false

  accepts_nested_attributes_for :recipe_steps, :reject_if => lambda { |a| a[:amount].blank? }, :allow_destroy => true

  define_index do
    indexes :name
    indexes ingredients.name as: :ingredient_name
  end
end

Drink Controller index

def index
    @drinks = Drink.search(params[:search])
    if current_user
      @cabinet = Cabinet.find(current_user.id)
    end
  end

Drink index.haml

= form_tag drinks_path, method: :get do
  .field
    = text_field_tag :search, params[:search]
    = submit_tag "Search", name: nil

- @drinks.each do |drink|
  = drink.name
BrianJakovich
  • 1,604
  • 1
  • 13
  • 23
  • 2
    Did you run `rake ts:index`? And if you're getting any errors - what are they about? – JazzJackrabbit Feb 12 '13 at 21:28
  • I'm not getting any errors from running rake ts:index I get: indexing index 'drink_core'... collected 2 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 2 docs, 16 bytes total 0.015 sec, 1018 bytes/sec, 127.33 docs/sec skipping non-plain index 'drink'... total 3 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg total 9 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg` – BrianJakovich Feb 12 '13 at 21:35
  • Nevermind, I think I figured this out. I need to re index after the database gets populated with new fields... What is the best way of making sure this happens whenever a new record is added? – BrianJakovich Feb 12 '13 at 21:41
  • 3
    The way I've done it is using crontab to periodically run `rake ts:rebuild`. And you can use the whenever gem to script the cron task when you deploy. – Tom L Feb 12 '13 at 21:45
  • 1
    I personally use `whenever` with `every :hour` setting. – JazzJackrabbit Feb 12 '13 at 21:49

1 Answers1

1

I was able to answer my own question. The problem was that I hadn't re-indexed after added records to the database. Thus they were not showing up when I was trying to print them out in my block.

As TomL suggested, the best way of dealing with this is to have a cron job periodically run

rake ts:rebuild
BrianJakovich
  • 1,604
  • 1
  • 13
  • 23