sphinx_attributes not defined

30 views
Skip to first unread message

J. Garcia

unread,
Nov 22, 2013, 12:03:28 PM11/22/13
to thinkin...@googlegroups.com
Hi there,

I'm trying to access sphinx_attributes in this setup:

class Brand < ActiveRecord::Base

  has_many :brand_ids
  has_many :brand_roles, :through => :brand_ids
end

ThinkingSphinx::Index.define :brand, :with => :active_record do
  indexes name, :sortable => true

  has brand_roles.id, :as => :role_id, :type => :integer, :multi => true
end

brands = Brand.search(:conditions => {:name => params[:search]}, :order => :name, :page => params[:page], :per_page => 5)
brands.context[:panes] << ThinkingSphinx::Panes::AttributesPane

puts brands[0].sphinx_attributes['role_id']

undefined method `sphinx_attributes' for #<Brand:0x5c19e88>

What am I missing?

My versions:
Sphinx 2.1.3
thinking-sphinx 3.0.6
Rails 3.2.14

Pat Allan

unread,
Nov 22, 2013, 6:36:21 PM11/22/13
to thinkin...@googlegroups.com
Are you running that test in the console? Be aware that panes are applied before the search query happens, but because the console calls inspect on each returned value, brands is evaluated as soon as you define it.

A way to get around that is add ; ‘’ to the end of that line - thus, an empty string is returned instead. This means you can then add the pane, and *then* accessing attributes in search values should work.

  brands = Brand.search(:conditions => {:name => params[:search]}, :order => :name, :page => params[:page], :per_page => 5); ''
  brands.context[:panes] << ThinkingSphinx::Panes::AttributesPane
  puts brands[0].sphinx_attributes['role_id']

If this doesn’t help, let me know.

— 
Pat

--
You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thinking-sphi...@googlegroups.com.
To post to this group, send email to thinkin...@googlegroups.com.
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/groups/opt_out.

J. Garcia

unread,
Nov 25, 2013, 4:08:07 AM11/25/13
to thinkin...@googlegroups.com
Thanks Pat. It worked.
Can this be called from within a model instance method?
I tried and it did not work, but I don't quite understand why:

undefined local variable or method `sphinx_attributes' for #<Brand:0x76ccce0>



--
You received this message because you are subscribed to a topic in the Google Groups "Thinking Sphinx" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/thinking-sphinx/Om7jB03FF9Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to thinking-sphi...@googlegroups.com.

Pat Allan

unread,
Nov 25, 2013, 6:15:04 AM11/25/13
to thinkin...@googlegroups.com
Nope, it’s available only on a wrapper object around each search result - the underlying model remains untouched (I’m not keen for TS to muck about with objects that don’t belong to it any more than necessary).

— 
Pat
Reply all
Reply to author
Forward
0 new messages