I’m coming from a PHP world and pretty new to Ruby, so there may be a better way to do this. Could this block be more succinctly expressed with better code?
bands = Band.where(:type => 'Rock & Roll').only(:id)
band_ids = bands.map &:id
band_ids.each do |id|
lead_singer = LeadSinger.find(:band_id => id)
if lead_singer
lead_singer.rock_and_roll = true
lead_singer.save
end
end
It all feels a little bloated. The “if lead_singer” part I added in case no result was found, but if there’s a better way to go about this, I’d love to be enlightened.
EDIT
I’m using MongoDB with Mongoid, so joins won’t be an option for me.
With MongoId you should still be able to update with criterias: