ruby-on-rails – Rails模型中关联,范围,验证等的正确顺序是什么

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails模型中关联,范围,验证等的正确顺序是什么前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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

这是正确的顺序吗?这对许多人来说可能并不那么重要,但我个人认为如果有这样的约定会很好.

@R_403_323@

没有这样的惯例.但是您可以为您的项目创建一个并在所有模型中与之保持一致.这就是我所遵循的.
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
原文链接:https://www.f2er.com/ruby/268077.html

猜你在找的Ruby相关文章