2008-01-31 02:50:15 -05:00
|
|
|
class Owner < ActiveRecord::Base
|
2011-11-29 13:58:41 -05:00
|
|
|
self.primary_key = :owner_id
|
2013-04-03 23:39:09 -04:00
|
|
|
has_many :pets, -> { order 'pets.name desc' }
|
2009-03-09 09:42:51 -04:00
|
|
|
has_many :toys, :through => :pets
|
2013-08-26 13:15:37 -04:00
|
|
|
|
2014-06-02 22:07:02 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-08-26 13:15:37 -04:00
|
|
|
after_commit :execute_blocks
|
|
|
|
|
2013-03-25 10:58:23 -04:00
|
|
|
accepts_nested_attributes_for :pets, allow_destroy: true
|
|
|
|
|
2013-08-26 13:15:37 -04:00
|
|
|
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
|
2009-03-09 09:42:51 -04:00
|
|
|
end
|