Model Name Conflict - ruby-on-rails

I have a model by the name Filter but due to the changes in reorganizing the filters from 2.0.3, it is conflicting with the
ActionController::Filters::Filter (class)
In my filters_controller.rb when I try to find the filter
Filter.find(:id)
as rails is infering the ActionController::Filters::Filter class rather than my model class Filter. Is there any work around other than renaming my model?
BTW: If I use ActiveRecord::Base::Filter.find(:id) to load my filter object, its working, but I am not quite sure if there are any unforseen implications by doing this, when rails try to unload/reload constants.
Thanks in advance.

The safest way to deal with this is to rename your model. Otherwise you run the risk of being 'clever' and getting bitten by this later when it will be very difficult to debug.

I almost missed your comment, so to answer your own question.
::Filter.find()

Related

Rails model/field naming

I'm about to add a model to my app and I was inclined to call it "collection". It was also to have a field called "status" but it occurred to me that I think Rails uses both those terms elsewhere and this might be a problem worth avoiding.
Should I pick another model name instead of collection? and would state be better than status?
Thanks.
I've used both before - I think you should use whatever makes most sense to you, that aren't reserved words. If you think they will conflict with logic elsewhere in your app, then you might consider a different model name and/or column name.

Any reason to avoid word "type" in Rails using Mongoid?

I know type is a magic field name with ActiveRecord for use in Single Table Inheritance. But :type is not included in Mongoid.destructive_fields. Is there any reason I can't use it as a field name? Upload.upload_type feels so ackward and I'd much prefer to use Upload.type.
Furthermore class, category, and variety don't seem like good alternatives.
I am using it without any problems so far. So unless I run into any issues down the road I'm going to answer no, there's no reason to avoid using the word "type".
In Mongoid an attribute _type is define to help in the STI too. So it's better to avoid using it too.
You are correct that "_type" is used for STI and thus "type" is technically a valid attribute name. It will work for your situation just fine. I would just be cautious using those "special names", however. If you ever switch over to a different database you will probably run into problems, but if you have control over that and know that you'll be using MongoDB, then there isn't a really good reason why you can't use the "type" field.
I personally would not use any of the special words that many databases or frameworks use, as it just makes it less-confusing for others. But that is just a personal preference; there's no technical reason.

Is a table with constant values in rails a good idea? or is a constant hash smarter?

I am creating an application where I need to categorize one of my models. There are five categories and they will not change, at least not for now. The object can only have one category at once. My two ideas are:
Create a whole table for adding the categories and add them in the migration file. I would then store the category id in the db for my object. Read about this causing problems with testing so I'm not sure. And, it seems a bit overkill.
Add a constant hash in my model for hosting the categories. I would then store the int key in my object.
Is there any better way I have not thought about? Are there any plugins for creating enums in rails?
Thanks
I have used this plugin https://github.com/adzap/active_enum some time ago and I think it works OK. You should definitely try it.

Thinking Sphinx - Already have a search method in the model?

I'd like to use Thinking Sphinx, but I keep having problems because I have a very large rails project and the search method is used in many of my models. These already existing search methods conflict with Thinking Sphinx's search method. Is there any way around this?
I'm talking thousands of lines of code I would have to change if I had to change my search method to something else. I can't seem to find a way to change the default search method in Thinking Sphinx though either.
Thanks.
Just answered this on the TS list, but happy to answer here as well :)
There isn't any inbuilt way to do this, but in theory it could be possible. Firstly - Thinking Sphinx adds the class-level search method when you call define_index on a model - so, if you define your own search method after that, it'll overwrite the Thinking Sphinx version.
This means you could just define a new method that does the same thing - here's the code for Model.search:
def self.search(*args)
ThinkingSphinx::Search.new *search_options(args)
end
Which you could easily rename to something else:
def self.sphinx_search(*args)
ThinkingSphinx::Search.new *search_options(args)
end
The one possible catch with this is that Thinking Sphinx may have expectations internally on the search method existing and behaving as normal. I'm not sure - but give this a spin and see how you go!
Update:
As it turns out, the above suggestion doesn't cover all situations and it's still buggy. So, I think the fallback solution is to fork Thinking Sphinx, change the method names, and use your version instead of the canonical one.

Rails naming conventions for models with forbidden names

I'm writing a rails application, and I need to name one of my model Test, and inside that model I need a class attribute - when I tried the first one, I couldn't run any UT since Test ceased to be a module - so I ranamed to Tst. I haven't even tried naming a column class - I went with clss. What names would you use? Are there any conventions known amongs RoR developers for such situations?
I haven't seen anything for Test but class is typically changed to klass.
Ruby and Rails have several different objects part of a standard library. You cannot use an already-defined class name as name for your models.
For instance, you cannot call a model Thread or Object because Ruby already defines a Thread and Object class.
Likewise, Ruby has a library called Test::Unit so you can't use the Test namespace as model name.
There isn't a real full list of reserve objects because it really depends on your current environment. However, in order to successfully use Rails, you should at least have a basic Ruby knowledge so that you know the names of the most common standard library classes.
I've run up against this a few times (writing educational software--where you regularly want to model 'tests' :-). Depends exactly what you're modeling I suppose, but I usually opt for 'quiz' to avoid conflicts. I'd love to hear a better solution, since I find 'quizzes' an awkward plural.
Like dj2 said, class is usually done as 'klass'.
Please check the following page for Rails reserved words: http://reservedwords.herokuapp.com/ None of these words should be used as class or attribute name.

Resources