Undefined method 'gsub' for symbol

630 views
Skip to first unread message

David Oliver

unread,
May 14, 2012, 7:01:21 AM5/14/12
to ruby-s...@googlegroups.com
Hi there

I am writing an api layer to serve as a passthrough for Solr searches. I won't go into the reasoning for this now, but the result is a class I'm calling Hit that is not an ActiveRecord. As the name suggests, it's simply modeled on a "search result" from Solr. So, this thing doesn't have any ActiveModel trappings whatsoever.

That said, I have set up the "searchable" block in the normal way (though, I'm bypassing Sunspot's use of Solr's dynamic fields). When I conduct a search, and specify an 'fq' I get the error mention in the subject of this thread:

Hit.search do
  keywords "foo"
  with :myfield, "my value"
end

This is resulting in the following error: NoMethodError: undefined method `gsub' for :myfield:Symbol.

I see in the Scope class in the sunspot-rails dsl library where it's happening, but rather than follow the trail, I thought I'd ask and see if I get a quicker response than my searching will yield. Any ideas?

-David

David Oliver

unread,
May 14, 2012, 8:38:08 AM5/14/12
to ruby-s...@googlegroups.com
I found out what my problem was. In "to_positive_boolean_phrase" the Restriction module (in the Base class) tries to detect the Solr field name and then escape it via Rsolr. All of my fields are explicitly mapped to "static" fields in the solr schema:

...
searchable do
  string :my_field, :as => :my_field
...

But I neglected to learn how to properly specify this. Turns out, the ":as => " bit should be a string, not a symbol. So, the above should be:

...
searchable do
  string :my_field, :as => "my_field"
...

This raises an interesting question though. The Sunspot and Sunspot Rails gems are clearly opinionated in favor of: Adding search to an existing Rails object domain. However, I would like to know if there's a easier way to take advantage of Sunspot's beautiful dsl without being so tightly coupled to things like, solr dynamic fields, and ActiveModel components. Is there another gem that does this? Like a lightweight version of Sunspot, or just the Sunspot dsl, spun off...something like that?

-David

Andy Lindeman

unread,
May 14, 2012, 9:10:53 AM5/14/12
to ruby-s...@googlegroups.com
On Mon, May 14, 2012 at 8:38 AM, David Oliver <dave...@gmail.com> wrote:
> But I neglected to learn how to properly specify this. Turns out, the ":as
> => " bit should be a string, not a symbol. So, the above should be:
>
> ...
> searchable do
>   string :my_field, :as => "my_field"
> ...

Side note: I'd accept a pull request to allow symbols as values for
:as. Could be as simple as `#to_s`'ing the value.

--
Andy Lindeman
http://www.andylindeman.com/

David Oliver

unread,
May 16, 2012, 10:21:39 AM5/16/12
to Sunspot
Pull request has been submitted. Thanks, Andy!

https://github.com/sunspot/sunspot/pull/226

-David

On May 14, 8:10 am, Andy Lindeman <alinde...@gmail.com> wrote:

David Oliver

unread,
May 22, 2012, 8:38:02 AM5/22/12
to Sunspot
Committed a better fix, if you want to take a look. Should be added to
the same pull request. Let me know if there are any issues with it.
Reply all
Reply to author
Forward
0 new messages