enum status: [ :in_progress, :on_hold, :cancelled, :shipped ]
我需要根据当前日期之前或之后的一年来拆分:shipped。
enum status: [ :in_progress, :on_hold, :cancelled, :shipped, :archive_shipped ]
如何写出条件的范围?还是应该将其编写为self.phase方法?
编辑
class SalesOrder < ActiveRecord::Base
include Filterable
include Tenancy
enum status: [ :in_progress, :on_hold, :cancelled, :shipped ]
has_many :line_items, dependent: :destroy
has_many :films
accepts_nested_attributes_for :line_items, allow_destroy: true
validates :code, presence: true,
uniqueness: { case_sensitive: false,
scope: :tenant_code }
validates :ship_date, presence: true, if: Proc.new { |o| o.shipped? }
validate :ship_date_after_release?
validates :release_date, presence: true
validates :due_date, presence: true
include PgSearch
pg_search_scope :search, against: [:code, :customer, :ship_to, :note],
:using => { tsearch: { prefix: true } }
scope :order_by, ->(col, dir) { order("#{col} #{dir}") }
scope :status, ->(status) { where(status: statuses[status]) }
scope :due_date_equals, ->(date) { where(due_date: date) }
scope :type, ->(prefix) { where('code ILIKE ?', prefix) }
scope :ship_date_before, ->(date) { where("ship_date <= ?", date) }
scope :ship_date_after, ->(date) { where("ship_date >= ?", date) }
scope :text_search, ->(query) { reorder('').search(query) }
scope :code_like, ->(code) { where('code ILIKE ?', code.gsub('*', '%')) }
scope :status_not, ->(status) { where("status <> ?", statuses[status]) }
添加:已存档 范围:装运,->(日期){其中(“ 1年之前的日期”) 作用域:archive_shipped,->(日期){其中(“一年后的日期”)