Ask for Help
“How can I know all the descendants of a class in the superclass at load time in ruby? I want to create a scope for each subclass.”
You can’t.
“What popovers should I use?”
We’ve used at least these two before:
“What recurring billing system should I use?”
We’ve had good luck with BrainTree and Recurly.
Interesting Things
- backbone-forms is a plugin to backbone that provides model-based form views similar to rails’
form_forfunctionality. - EmberJS meetup will be Feb 21, at our new pivotal office!
You *could* use the `inherited` hook:
class Superklass
def self.inherited(subclass)
puts “lol, #{subclass} just inherited from me”
end
end
class Subclass1 < Superklass
end
class Subclass2 < Superklass
end
prints this:
lol, Subclass1 just inherited from me
lol, Subclass2 just inherited from me
You probably shouldn’t tho.
February 9, 2012 at 3:34 pm
As the previous commenter points out, you very much can, unless we’re misunderstanding the question.
February 9, 2012 at 7:40 pm
We want to define named scopes in the superclass at load time, one for each subclass. Something like this won’t work:
class A < ActiveRecord::Base
subclasses.each do |subclass|
named_scope "for_#{subclass}", where(:type => subclass)
end
end
Because the subclasses haven’t all been defined at the time of load. The `self.inherited` callback will probably work to dynamically define named scopes for each subclass. The problem used to be ensuring that all the subclasses are loaded before the scopes are invoked, but we can solve that with eager loading of classes in rails. Thanks!
February 9, 2012 at 10:10 pm