which below is more effective for this task. I want to loop through the model, check if the list consisting of the whole shafts associated with each model_id is greater than 0. If then we take the corresponding models into the list of models.
@models = Model.find(:all).collect{|m| m }.reject{ |i| modellist[i.id] < 1 }
or how is it
finalModels = [] Model.find_each do |model| if modellist[model.id] > 0
Im leaning towards the second approach, but not sure. Perhaps some insight into how .collect and .reject work in order to understand how effective it is.
My model is called Picture. modellist (or pList) contains data like this.
[nil,nil,nil,3,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil, nil,nil,7,nil,nil,nil,0,nil,nil,nil,0,0,nil,nil,1,3]
I pList index number corresponds to the image id for this. So I need to find pics where pList [image id] is greater than 0.
Used by Benoit Garrets. I needed to make sure that pList was declared pList = Hash.new, and not pList = []. the exact request i used was
@pictures = Picture.find(pList.select {|k, v| v > 0}.keys)