Notes posted by lazylester
RSS feedA named_scope also responds to model class methods
for instance
class Student < ActiveRecord::Base
named_scope :sophomore, :conditions => 'year=2' def self.eligible_to_vote select{|s| s.age >= 18} end end ss = Student.sophomore.eligible_to_vote
Rails documentation for nested attributes
ActiveRecord/NestedAttributes/ClassMethods
(don’t follow this link, the url interpreter isn’t rendering it correctly :(, but the correct link is at the top of this page)
extend adds class methods too
Because classes are objects. So for example:
module Ispeak def says "greetings aliens!" end end module Ieat def eats "spinach" end end module Inhabitant def says "I'm strong to the finish" end end class Human extend Ispeak # add class methods from Ispeak include Inhabitant # add instance methods from Inhabitant end Human.extend Ieat # add class methods from Ieat puts Human.says # -> greetings aliens! puts Human.eats # -> spinach popeye = Human.new puts popeye.says # -> I'm strong to the finish
Caveat and design hints regarding :counter_cache
(From Obie Fernandez/ The Rails Way, ISBN 978-0321445612. Thanks Obie!)
This caveat:
The value of the counter cache column must be set to zero by default in the database! Otherwise the counter caching won’t work at all. It’s because the way that Rails implements the counter caching behavior is by adding a simple callback that goes directly to the database with an UPDATE command and increments the value of the counter.
And these tips:
If a significant percentage of your association collections will be empty at any given moment, you can optimize performance at the cost of some extra database storage by using counter caches liberally. The reason is that when the counter cache attribute is at zero, Rails won’t even try to query the database for the associated records!
If you’re not careful, and neglect to set a default value of 0 for the counter cache column on the database, or misspell the column name, the counter cache will still seem to work! There is a magic method on all classes with has_many associations called collection_count, just like the counter cache. It will return a correct count value if you don’t have a counter cache option set or the counter cache column value is null!
Comparing Date with Numeric in mixed sort
While:
Date#<=>(other)
can accept a Numeric object as other, the reverse is not true:
Numeric#<=>(other)
cannot accept a Date object as other.
So if you are sorting a list containing a mix of dates and numbers, you can get different results depending on the starting order!
a = Date.parse("2008-01-01") b = Date.parse("2009-10-22") c = Date.parse("2005-01-04") d = 0 [a,b,c,d].sort #=> [0, Tue, 04 Jan 2005, Tue, 01 Jan 2008, Thu, 22 Oct 2009] [b,c,d,a].sort #=> ArgumentError: comparison of Fixnum with Date failed
use #collect instead of #each
The earlier reminder to use #collect instead of #each applies regardless of whether the tag is nested or not.
This is counterintuitive, as #collect returns an array of strings of HTML tags, but ActionView renders it properly.


