1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/models/owner.rb
Ladislav Smola cb4f6875b6 Fix undefined method `owners' for NullPreloader:Class
* Fix undefined method `owners' for NullPreloader:Class

Fixing undefined method `owners' for
ActiveRecord::Associations::Preloader::NullPreloader:Class

* Use Ruby 1.9 hash format

Use Ruby 1.9 hash format

#24192

[Rafael Mendonça França + Ladislav Smola]
2016-04-06 00:46:55 -03:00

37 lines
764 B
Ruby

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