How to avoid namespace activerecord models?
activerecord, ruby-on-rails-3
Solution
There is a problem with models in subdirectories and associations in Rails 3. I also have stumbled upon this.
My solution was to point an explicit :class_name for every association to model in subdirectory, like
class Listing < ActiveRecord::Base
has_many :communications, :class_name => "::Communication"
end
Take notice of using "::" before model name -- it says to rails that there is no namespace for Communication model.
Problem
I'm using rails 3.2.3 with more than 100 models. The thing is that the directory app/models is too crowded. I organized the directory into several groups and add autoload_paths (for the new sub directories). I don't want my models to use namespace, because it would end up into several namespaces which is not good for development. Let's say: ``` # app/models/listing.rb class Listing < ActiveRecord::Base has_many :communications end # app/models/listing/communication.rb class Communication < ActiveRecord::Base end ``` In my rails console, it works for any models with absolute reference except in the activerecord associations. I cannot call Listing.first.communications. I see what it does it is trying to load Listing::Communication, and it failed out because the content of this file is Communication (without namespace). ``` LoadError: Expected /home/chamnap/my_app/app/models/listing/communication.rb to define Listing::Communication ``` Is there a way to group models into directory and use them without namespace? Or is there a way to preload all models so that Rails doesn't load model on the fly?