Sha256: 40ebee401b35a1bfcb1a31a612f209bd458c82ed51cc8ed9a32c84d8a88055e2

Contents?: true

Size: 705 Bytes

Versions: 2

Compression:

Stored size: 705 Bytes

Contents

class Owner < ActiveRecord::Base
  self.primary_key = :owner_id
  has_many :pets, -> { order 'pets.name desc' }
  has_many :toys, :through => :pets

  belongs_to :last_pet, class_name: 'Pet'
  scope :including_last_pet, -> {
    select(%q[
      owners.*, (
        select p.pet_id from pets p
        where p.owner_id = owners.owner_id
        order by p.name desc
        limit 1
      ) as last_pet_id
    ]).includes(:last_pet)
  }

  after_commit :execute_blocks

  def blocks
    @blocks ||= []
  end

  def on_after_commit(&block)
    blocks << block
  end

  def execute_blocks
    blocks.each do |block|
      block.call(self)
    end
    @blocks = []
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ibm_db-3.0.5-x86-mingw32 test/models/owner.rb
ibm_db-3.0.5 test/models/owner.rb