Ruby on Rails中的ActiveRecord编程指南

2019-09-25 09:39:52于丽


    避免改动缺省的 ActiveRecord(表的名字、主键,等等),除非你有一个非常好的理由(像是不受你控制的数据库)。
    把宏风格的方法放在类别定义的前面(has_many, validates, 等等)。

    偏好 has_many :through 胜于 has_and_belongs_to_many。 使用 has_many :through 允许在 join 模型有附加的属性及验证

   

 # 使用 has_and_belongs_to_many
  class User < ActiveRecord::Base
   has_and_belongs_to_many :groups
  end

  class Group < ActiveRecord::Base
   has_and_belongs_to_many :users
  end

  # 偏好方式 - using has_many :through
  class User < ActiveRecord::Base
   has_many :memberships
   has_many :groups, through: :memberships
  end

  class Membership < ActiveRecord::Base
   belongs_to :user
   belongs_to :group
  end

  class Group < ActiveRecord::Base
   has_many :memberships
   has_many :users, through: :memberships
  end

    使用新的 "sexy" validation。

    当一个惯用的验证使用超过一次或验证是某个正则表达映射时,创建一个惯用的 validator 文件。

  # 差
  class Person
   validates :email, format: { with: /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i }
  end

  # 好
  class EmailValidator < ActiveModel::EachValidator
   def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || 'is not a valid email') unless value =~ /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i
   end
  end

  class Person
   validates :email, email: true
  end

    所有惯用的验证器应放在一个共享的 gem 。

    自由地使用命名的作用域(scope)。

   

 class User < ActiveRecord::Base
   scope :active, -> { where(active: true) }
   scope :inactive, -> { where(active: false) }

   scope :with_orders, -> { joins(:orders).select('distinct(users.id)') }
  end

    将命名的作用域包在 lambda 里来惰性地初始化。

 

  # 差劲
  class User < ActiveRecord::Base
   scope :active, where(active: true)
   scope :inactive, where(active: false)

   scope :with_orders, joins(:orders).select('distinct(users.id)')
  end

  # 好
  class User < ActiveRecord::Base
   scope :active, -> { where(active: true) }
   scope :inactive, -> { where(active: false) }

   scope :with_orders, -> { joins(:orders).select('distinct(users.id)') }
  end

    当一个由 lambda 及参数定义的作用域变得过于复杂时,更好的方式是建一个作为同样用途的类别方法,并返回一个 ActiveRecord::Relation 对象。你也可以这么定义出更精简的作用域。

  class User < ActiveRecord::Base
   def self.with_orders
    joins(:orders).select('distinct(users.id)')
   end
  end