i have collection contains class like:
locations = location.all class location < activerecord::base end
the location class has property: code
i wan remove item collection if code == "unused".
how many different ways can in ruby?
i doing this:
locations = location.all.select { |l| l.code != "unused" }
this works great wondering other ways learning purposes (if there big performance advantages in way know also).
update please ignore fact loading collection database, wasn't point. want learn how remove things in-memory not simple clauses :)
you can fetch records database need:
rails 4 onwards:
locations = location.where.not(code: "unused")
before rails 4:
locations = location.where("code != ?", "unused")
if have collection , want reject items it, can try this:
locations.reject! {|location| location.code != "unused"}
Comments
Post a Comment