I'm not sure I understand your question. Are you looking for something like this?
def self.restock_energy_potions market = find_or_create_market potions = EnergyPotion.find_all_by_user_id(market.id) (potions.size...5).each {EnergyPotion.new(:user_id => market.id).save } end end
Note the triple points in the range; You do not want to create a potion if there are already 5 of them.
In addition, if your potions were linked (for example, has_many ), you could create them through the market.potions property (I assume here, about the relationship between users and markets), the details depend on how your models are set up) and save them all at once. I do not think that saving on the database would be significant.
source share