Created
September 24, 2010 15:10
-
-
Save chollier/595525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def subproduct? | |
!self.parent.blank? | |
end | |
def self.not_sub | |
returning [] do |value| | |
Product.each do |x| | |
if !x.subproduct? | |
value << x | |
end | |
end | |
end | |
end |
3eme : Vu que tu construit un Array a partir d'un autre, inject peut etre utilisé. Ex :
def self.not_sub Product.each.inject([]) { |value, product| value << x if !product.subproduct?; value; } end
4eme : sinon effectivement ces commentaires sont pur ruby, y a moyen de faire en SQL.
En SQL c'est mieux en effet :
scope :not_sub, lambda {
joins("left join product_joins on products.id = product_joins
.sub_product_id
").where('product_joins.product_id' => nil)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2ème commentaire : returning est deprecated. Il faut utiliser la méthode tap désormais.
Donc a la place de returning([]) do..., utilise [].tap do...