The more performant way would be to call compact on the array resulting from
the collect:
def translate(object, attribute_value)
objects = source_objects(object)
return nil if objects.nil? || objects.empty?
if objects.length > 1
objects.collect { |item| item.send(column.__name) }.compact.detect { |
item|
item.to_crc32 == attribute_value
}
else
method = value_source || column.__name
objects.first.send(method)
end
end
Otherwise (in the worst case scenario where the detect hits for the last
element or not at all) you'd have to iterate the same (potentially large
collection) twice.
- C.
On May 4, 2011, at 1:36 AM, Alex Chee wrote:
> I'm trying to make a facet from a string column that might contain
> NULL. When it gets to the translate method, it tries to call to_crc32
> on nil:
>
> NoMethodError: undefined method `to_crc32' for nil:NilClass
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/gems/
> activesupport-3.0.6/lib/active_support/whiny_nil.rb:48:in
> `method_missing'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet.rb:106:in `block in
> translate'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet.rb:105:in `each'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet.rb:105:in `detect'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet.rb:105:in `translate'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet.rb:81:in `value'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet_search.rb:127:in `block
> in add_from_results'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/search.rb:322:in `block in
> each_with_match'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/search.rb:321:in `each'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/search.rb:321:in
> `each_with_index'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/search.rb:321:in
> `each_with_match'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet_search.rb:126:in
> `add_from_results'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet_search.rb:52:in `block
> in populate'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet_search.rb:49:in `each'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet_search.rb:49:in
> `each_with_index'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet_search.rb:49:in
> `populate'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/facet_search.rb:13:in
> `initialize'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/search_methods.rb:422:in `new'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/bundler/gems/thinking-
> sphinx-f81441e28813/lib/thinking_sphinx/search_methods.rb:422:in
> `facets'
> from (irb):1
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.6/
> lib/rails/commands/console.rb:44:in `start'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.6/
> lib/rails/commands/console.rb:8:in `start'
> from /Users/alexchee/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.6/
> lib/rails/commands.rb:23:in `<top (required)>'
> from script/rails:6:in `require'
> from script/rails:6:in `<main>'irb(main):002:0>
>
> So I'm trying to get around this by changing the translate method to
> only collect if the item is not nil and the crc32 matches, like:
> def translate(object, attribute_value)
> objects = source_objects(object)
> return nil if objects.nil? || objects.empty?
>
> if objects.length > 1
> objects.collect { |item| item.send(column.__name) }.detect { |
> item|
> !item.nil? && item.to_crc32 == attribute_value
> }
> else
> method = value_source || column.__name
> objects.first.send(method)
> end
> end
>
> Does anyone know if this sounds like reasonable solution for me?
>
> Thanks,
> -Alex
>
> --
> You received this message because you are subscribed to the Google Groups
> "Thinking Sphinx" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/thinking-sphinx?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en.