Rails就是’Convention over Configuration’.但是,我还没有在Rails模型中遇到关联,范围,包含,验证等的顺序的“标准”.以下是简化的产品型号:
class Product < ActiveRecord::Base mount_uploader :logo,AssetUploader acts_as_taggable paginates_per 50 include ActionView::Helpers::NumberHelper belongs_to :company validates_presence_of [:title,:price,:plu] scope :on_website,where(display: true) def display_price ... end end
这是正确的顺序吗?这对许多人来说可能并不那么重要,但我个人认为如果有这样的约定会很好.
解决方法
没有这样的惯例.但是您可以为您的项目创建一个并在所有模型中与之保持一致.这就是我所遵循的.
class Model < ActiveRecord::Base #all mixins include Something extend Something #other stuff acts_as_taggable paginates #associations has_many :something belongs_to :something_else #validations validate_presence_of :something #scopes scope :something #instance methods def instance_method end #class methods def self.method end #private methods private def method2 end end