Sha256: 2df0e4c15141781ebee70624bd95702a25aeb70003e4d575033d706d63adfd8d

Contents?: true

Size: 764 Bytes

Versions: 8

Compression:

Stored size: 764 Bytes

Contents

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

  accepts_nested_attributes_for :pets, allow_destroy: true

  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

8 entries across 8 versions & 1 rubygems

Version Path
ibm_db-5.2.0 test/models/owner.rb
ibm_db-5.1.0 test/models/owner.rb
ibm_db-5.0.5 test/models/owner.rb
ibm_db-5.0.4 test/models/owner.rb
ibm_db-5.0.3 test/models/owner.rb
ibm_db-5.0.2 test/models/owner.rb
ibm_db-4.0.0-x86-mingw32 test/models/owner.rb
ibm_db-4.0.0 test/models/owner.rb