Hi Kyle
The defined indices are lazily loaded, so you’ll need to call preload_indices
on the configuration instance before enumerating through each index. The error
you’re seeing is from when - somewhere else in the TS code - preload_indices
has been called, and thus all indices, including distributed indices are
available. Distributed indices, by their nature, don’t have their own
attributes, so you’ll want to ignore those.
One way of doing this:
ThinkingSphinx::Configuration.instance.preload_indices
index = ThinkingSphinx::Configuration.instance.indices.reject { |index|
index.distributed?
}.detect { |index| index.model == klass }
index.attributes.any? { |attribute| attribute.name == ‘updated_at’ }
Or, using a bit more of TS’s internals:
index = ThinkingSphinx::IndexSet.new(classes: [klass]).to_a.first
index.attributes.any? { |attribute| attribute.name == ‘updated_at’ }
Indices returned by the IndexSet class have distributed indices filtered out,
and calls preload_indices for you.
Cheers
—
Pat
> On 5 Jun 2016, at 10:38 AM, Kyle Ginavan <[email protected]> wrote:
>
> Problem:
> In our application we also have a generic search function that uses many
> different indices. Each index can be sorted differently. Therefore, we want
> to examine the indices to determine if certain attributes are present and can
> be sorted. We are attempting to use a line of code like:
>
> ThinkingSphinx::Configuration.instance.indices.detect { |i| i.model == klass
> }.attributes.any? {|a| a.name == "updated_at" }
>
> Issue:
> The above works great most of the time. However, if we change the file where
> this line of code exists more than 2 times (3 or more times) we receive the
> below error:
>
> NoMethodError - undefined method `attributes' for
> #<ThinkingSphinx::Distributed::Index:0x007facb051d268>:
>
>
>
> I assume there is some sort of Rails development class loading issue going on
> here and I have no idea why it is the 3rd time where it fails. I'm unsure if
> this same issue happens in production.
>
>
>
> Question:
>
> My question is #1 do you see a better way to solve this problem or #2 is this
> a bug that needs logged?
>
>
> Stack:
> Rails: 4.2.5.1
> Ruby: 2.2.3
> OS: Linux & OSX
> Thinking Sphinx: 3.1.3
> Sphinx: 2.2.10
>
> --
> 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 [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/thinking-sphinx
> <https://groups.google.com/group/thinking-sphinx>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/d/optout.