Skip to content

Instantly share code, notes, and snippets.

@kalabiyau
Created November 19, 2011 17:09
Show Gist options
  • Save kalabiyau/1379057 to your computer and use it in GitHub Desktop.
Save kalabiyau/1379057 to your computer and use it in GitHub Desktop.
#В общем в той истории с super все не совсем так.
#Вот как оно работает
class Lease < ActiveRecord::Base
def rate
(tenant && tenant.rate) ? tenant.rate : super
end
end
#Так оно работает не только потому что class_eval задефайнил метод rate
#который из ActiveRecord, в класс Lease,
#потому что scope поднимается в родителя (в суперкласс) Lease.
#А вот там этот метод задейфанен также в виде сслыки на таблицу.
#Иными словами:
class Lease
def rate
"Foo"
end
def rate
super
end
end
##irb>>
Lease.rate
#срайзит
NoMethodError: undefined method super
#потому что нет родителя у такого класса.
#Итого, получается, что в инстансе ActiveRecord::Basic, который принадлежит модели
#также
#определены атрибуты модели, что позволяет обращаться к
#ним через super
#Но такая тема :
class Meme < ActiveRecord::Base
def name
p "Green"
end
def name
super
end
end
#irb>>
Meme.create!(:name=> "Black")
#>>
Meme.last.name
#Вернет не Green как мы с тобой думали,
#а Black так как пойдет в superclass и найдет там метод name который дернет базу
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment