Help with HABTM

32 views
Skip to first unread message

Alan McCann

unread,
Nov 29, 2011, 11:55:24 PM11/29/11
to thinkin...@googlegroups.com
Hi;
I am new to ThinkingSphinx (coming from IndexTank). Except for two issues, it has been a smooth transition

Issue 1: HABTM

I have this model

class Item < ActiveRecord::Base
  has_and_belongs_to_many :groups
 define_index do
    indexes title
    indexes body
    has groups(:id), :as => :groups
 end
 Item.search(searchstring, :page => page,:conditions => {:groups => group)
 # I pass a group id into the search 
end

I keep getting this error 
ActionView::Template::Error (index item_core: query error: no field 'groups' found in schema):

I have tried has groups(:id), :as => :group_ids.

Nothing fixes the issue.

My second issue is how to test locally. I am deploying to heroku with flyingsphinx but I don't seem to be able to run tests with my local ts instance and it seems strange to have to push code to production to rerun the index to be able to test the changes.

Any advice is appreciated!

Alan

Pat Allan

unread,
Nov 30, 2011, 12:20:10 AM11/30/11
to thinkin...@googlegroups.com
Hi Alan

Nice to know things have mostly been working well. Here's a few tips for what you're trying to do.

First up - you've defined groups as an attribute, which makes sense - though I would probably call it group_ids, given it is a collection of ids. Next, to filter on attributes, you need to use :with, not :conditions (that's for fields only). You also need to pass through the actual id value, not a model.

So, with your first setup, the query should be as follows:

Item.search searchstring, :page => page, :with => {:groups => group.id}

When it comes to testing, it depends how you want to do this… I recommend stubbing out calls in controller/model tests, and then with Cucumber and/or Capybara acceptance tests, have a look at ThinkingSphinx::Test and this blog post:
http://freelancing-god.github.com/ts/en/testing.html#functional
http://freelancing-gods.com/posts/using_thinking_sphinx_with_cucumber

If there's any more questions, just let me know.

Cheers

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/xStJo722WZAJ.
> To post to this group, send email to thinkin...@googlegroups.com.
> To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.

Alan McCann

unread,
Nov 30, 2011, 1:04:25 AM11/30/11
to thinkin...@googlegroups.com
Thanks - the :with did the trick! My code didn't show how I was actually calling the search exactly and I was calling with the object ids.

Now I have discovered an issue with a conflict between will_paginate and kaminari in my app that is impacting the search results. I had been using will_paginate in my app controllers and kaminari came along with RailsAdmin. So now I just have to replace will_paginate and its associated code and I should be good to go. The side effect I am seeing is a weird page parameter and log entries of the .page method for my models being overridden.

I was imprecise in my second question:

In development mode, I have a local sphinx server and it seems I can access the flyingsphinx server. It seems to make sense to only access the local server during development but I wasn't sure if just putting the flyingsphinx gem in a production block in my Gemfile would allow me to do that. So I guess I'm looking for guidance as to how people set up there dev environments.

Alan

Pat Allan

unread,
Nov 30, 2011, 1:06:11 AM11/30/11
to thinkin...@googlegroups.com
Flying Sphinx can be ignored locally - if you want, just put it in your production env in your Gemfile - and use Thinking Sphinx normally.

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/bAsEn-VZOA0J.

Reply all
Reply to author
Forward
0 new messages