开发者

Thinking Sphinx index conflicting with Ruby method name

I ignorantly开发者_开发百科 named a model in my Rails app System, which is also a ruby core method. This model is in a relationship with another model Project, which I am trying to index.

Ideally, I am looking to setup my index like this:

define_index do
  indexes :name, :sortable => true
  indexes system(:name), :sortable => true, :as => :system_name
end

I could change the model name, but I'd call that a compromise, and I'm not convinced I need to. Is there a good work-around for this?

ruby 1.8.7, rails 3.0.7, thinking_sphinx 2.0.3


The good work around for naming variables or user-level Classes with reserved words (language keywords and platform-level methods/classes) is not do it in the first place.

The second best workaround is to use scoping :: to make sure the name you are calling is the one you want

::system()  # calls the actual system method as defined by Ruby
APPNAME::MODEL_NAME # would call the model defined as `APPNAME::MODEL_NAME`

I can't really think of a workaround without namespacing your models (although knowing Ruby, its more than possible some functionality exists-- just never needed it myself). Prolly all of them tbh since it would get even more confusing if only half your models were namespaced. In the long run, its just more typing remembering to namespace everything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜