Skip to content

Instantly share code, notes, and snippets.

@jacobdam
Created May 5, 2015 08:02
Show Gist options
  • Save jacobdam/184e99819841bcf903ce to your computer and use it in GitHub Desktop.
Save jacobdam/184e99819841bcf903ce to your computer and use it in GitHub Desktop.
Lazy-lize class loading dependency
# bad
class Book
belongs_to :categoy, :class_name => BookCategory
end
# good
class Book
belongs_to :categoy, :class_name => 'BookCategory'
end
# bad
class Institution
STRATEGY = SomeStrategy
def strategy
STRATEGY
end
end
# good
class Institution
STRATEGY = 'SomeStrategy'
def strategy
STRATEGY.constantize
end
end
# bad
class Book
scope :for_children, where(category_id: Category::CHILDRED_CATEGORY_IDS)
end
# good
class Book
scope :for_children, lambda {
where(category_id: Category::CHILDREN_CATEGORY_IDS)
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment