新手问题 ruby-china 源码的一个问题

kingkazma · 2013年02月18日 · 最后由 kingkazma 回复于 2013年02月18日 · 2122 次阅读

刚刚开始学习 ruby-china 源码,看到ruby-china將Model的一些通用方法放在了models/mongoid/base_model.rb 里的Mongoid::BaseModel模块里 请问这个模块怎么include进别的modle的? 如比在 user modle 中,为何 include Mongoid::BaseModel 就能找到这个模块? models/mongoid下的文件都是自动加载的么?

在 rails initialized 后,rb 文件已经按约定载入,所以你可以 include Mongoid::BaseModel 2.40 activesupport/lib/action_dispatch.rb This file attempts to locate the active_support and active_model libraries by looking a couple of directories back from the current file and then adds the active_support and active_model lib directories to the load path, but only if they aren’t already, which they are.

节选自: http://guides.rubyonrails.org/initialization.html#back-to-activesupport-lib-action_dispatch-rb

@xds2000 谢谢,我要好好看看rails的 Initialization 过程 另外请问下加载时候的命名规则是rails已经规定好的么? 比如我在 app/modles/user.rb 中 include Mongoid::BaseModle 这个模块 那rails就会自动加载 app/modles 里mongoid子目录下的 base_modle.rb 这个文件

你说的没错,看这里: The Rails philosophy includes several guiding principles:

DRY – “Don’t Repeat Yourself” – suggests that writing the same code over and over again is a bad thing. Convention Over Configuration – means that Rails makes assumptions about what you want to do and how you’re going to do it, rather than requiring you to specify every little thing through endless configuration files. REST is the best pattern for web applications – organizing your application around resources and standard HTTP verbs is the fastest way to go.

http://railscasts-china.com/episodes/rails-starter-c1-starter-here

需要 登录 后方可回复, 如果你还没有账号请 注册新账号