- def get_all_subclasses_of(string)
- raise ArgumentError.new("#{string} is not an AR Class") if eval(string).class.ancestors
- .collect {|e| e.to_s.downcase}.include? ("ActiveRecord::Base")
-
- return ObjectSpace.subclasses_of(eval(string))
- end
Monday, April 16, 2007
get classes of a class
Saturday, April 14, 2007
Thursday, April 12, 2007
Number to english
num_to_english is my first plugin, i kinda thought it would become a nice addition to the Fixnum class in Ruby.
num_to_english hooks up to the Fixnum class and enables the use of the to_english method as follows
eizesus@eizesus-desktop:~/Projects/testing/trunk$ script/console
Loading development environment.
>> 3.to_english
=> "three"
>> 12.to_english
=> "twelve"
>> 34.to_english
=> "thirty-four"
>> 100.to_english
=> "one hundred"
>> (100 + 321).to_english
=> "four hundred twenty-one"
>> 12312311.to_english
=> "twelve million, three hundred twelve thousand, three hundred eleven"
get it from http://svn.creopolis.com/num_to_english/trunk
num_to_english hooks up to the Fixnum class and enables the use of the to_english method as follows
eizesus@eizesus-desktop:~/Projects/testing/trunk$ script/console
Loading development environment.
>> 3.to_english
=> "three"
>> 12.to_english
=> "twelve"
>> 34.to_english
=> "thirty-four"
>> 100.to_english
=> "one hundred"
>> (100 + 321).to_english
=> "four hundred twenty-one"
>> 12312311.to_english
=> "twelve million, three hundred twelve thousand, three hundred eleven"
get it from http://svn.creopolis.com/num_to_english/trunk
Thursday, April 5, 2007
counter_cache
Many times when using a belongs_to relation, we encounter the need to size up that association, we can use 2 methods which are created for us, size and (association_pluralization)_count.
so if SuperHero has_many :super_powers than:
me = SuperHero.find(1)
me.super_powers.size
me.super_powers_count
they will both issue that same SQL statement at this point
SELECT count(*) as count_all FROM super_powers WHERE (super_hero_id = 1)
which basically preforms a SELECT * statement, we don't like it :)
the :counter_cache parameter for the belongs_to macro, indicates the use of a (association_pluralization)_count named column which will be incremented and decremented according to the use of the associative array, this can and does save a lot of time and DB - APP - DB roundup's.
so if SuperHero has_many :super_powers than:
me = SuperHero.find(1)
me.super_powers.size
me.super_powers_count
they will both issue that same SQL statement at this point
SELECT count(*) as count_all FROM super_powers WHERE (super_hero_id = 1)
which basically preforms a SELECT * statement, we don't like it :)
the :counter_cache parameter for the belongs_to macro, indicates the use of a (association_pluralization)_count named column which will be incremented and decremented according to the use of the associative array, this can and does save a lot of time and DB - APP - DB roundup's.