ruby on rails - How to "shift" objects from ActiveRecord array -


i have method

def gen_events(score)   events = location.events   (1..rand(5..7)).each |n|     random = rand(0.139..1).round(3)     rarity = character.get_rarity(random)     event = events.where(rarity: rarity).shuffle.shift #<-- here     self.events << event       end end 

currently, shift method grabs first element, doesn't remove it, how can go making both?

this not array: events.where(rarity: rarity), activerecord scope, can't remove things without destroying , erasing them database. instead, should keep array of object found, , use filter future results:

def gen_events(score)     events = location.events     new_events = []     (1..rand(5..7)).each |n|         random = rand(0.139..1).round(3)         rarity = character.get_rarity(random)         event = events.where(rarity: rarity).where.not(id: new_events.map(&:id).sample         new_events << event     end     self.events << new_events     end 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -