내가 레일 응용 프로그램에서 두 개의 클래스가 있다고 가정확장 액티브 레코드는 중복을 줄이기 위해
class Subject < ActiveRecord::Base
def children?
Subject.where(:parent_id => self.id).length > 0
end
def children
Subject.where(:parent_id => self.id)
end
end
class Region < ActiveRecord::Base
def children?
Region.where(:parent_id => self.id).length > 0
end
def children
Region.where(:parent_id => self.id)
end
end
은 무엇 중복 클래스 메소드를 줄일 수있는 가장 좋은 방법이 될 것입니까? 두 가지 새로운 방법으로 ActiveRecord를 확장 할 수 있습니까? 그렇다면 두 클래스 모두에서 사용할 수있는 두 가지 새로운 메소드를 어떻게 작성할 수 있습니까?
덕분에, 마이크
조금 리팩토링 수'children.size> 0 '=>'children.empty?': – fl00r
그래, 좋네요 .. 좋은 지적. 나는 당신이'! children.empty? ', 또는 훨씬 더 관용적이라고 가정한다. 'children.present? ' – idlefingers
+1'present? ': D – fl00r