How to avoid activetecord namespace models?

I use rails 3.2.3 with over 100 models. The fact is that the application / directory model is too crowded. I organized the directory into several groups and added autoload_paths (for new subdirectories). I don’t want my models to use a namespace because it falls into several namespaces that are not suitable for development. Let them talk:

# 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 an absolute reference, with the exception of activerecord associations. I can not name Listing.first.communications. I see that he is trying to download Listing :: Link, and it crashes because the contents of this file are Link (without a namespace).

LoadError: Expected /home/chamnap/my_app/app/models/listing/communication.rb to define Listing::Communication

? , Rails ?

+3
2

Rails 3. .

: class_name ,

class Listing < ActiveRecord::Base
  has_many :communications, :class_name => "::Communication"
end

"::" - , .

+5
# e.g. subscription/coupon defines ::Coupon, would would blow up with expected coupon.rb to define Subscription::Coupon
# to remove this:
# a: namespace the models
# b: move them to top-level
# c: add :class_name => "::Coupon" to all places where they are used as associations
ActiveRecord::Reflection::MacroReflection.class_eval do
  def class_name_with_top_level
    "::#{class_name_without_top_level}".sub("::::","::")
  end
  alias_method_chain :class_name, :top_level
end
+2

All Articles