Cha*_*nap 3 activerecord ruby-on-rails-3
我正在使用带有100多个型号的rails 3.2.3.问题是目录app/models太拥挤了.我将目录组织成几个组并添加autoload_paths(用于新的子目录).我不希望我的模型使用命名空间,因为它最终会变成几个名称空间,这对开发不利.让我们说:
# app/models/listing.rb
class Listing < ActiveRecord::Base
has_many :communications
end
# app/models/listing/communication.rb
class Communication < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
在我的rails控制台中,它适用于任何具有绝对引用的模型,但在activerecord关联中除外.我无法调用Listing.first.communications.我看到它正在尝试加载Listing :: Communication,它失败了,因为这个文件的内容是Communication(没有名称空间).
LoadError: Expected /home/chamnap/my_app/app/models/listing/communication.rb to define Listing::Communication
Run Code Online (Sandbox Code Playgroud)
有没有办法将模型分组到目录中并使用它们而不使用命名空间?或者有没有办法预加载所有模型,以便Rails不会动态加载模型?
Rails 3中的子目录和关联中的模型存在问题.我也偶然发现了这一点.
我的解决方案是指出一个显式的:class_name,用于在子目录中建模的每个关联,比如
class Listing < ActiveRecord::Base
has_many :communications, :class_name => "::Communication"
end
Run Code Online (Sandbox Code Playgroud)
注意在模型名称之前使用"::" - 它告诉rails,通信模型没有命名空间.
| 归档时间: |
|
| 查看次数: |
2116 次 |
| 最近记录: |