加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:13:40 所属栏目:百科 来源:网络整理
导读:Rails就是’Convention over Configuration’.但是,我还没有在Rails模型中遇到关联,范围,包含,验证等的顺序的“标准”.以下是简化的产品型号: class Product ActiveRecord::Base mount_uploader :logo,AssetUploader acts_as_taggable paginates_per 50 inc
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读