public
Last active

removing a bunch of mongo attributes in one hit #L19

  • Download Gist
nuke-attrs.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
class AddPageFields < Mongoid::Migration
def self.up
Account.all.each do |account|
account.pages.each do |page|
page.update_attributes!(
:callout => Account::Page::Defaults.callout,
:phone => Account::Page::Defaults.phone,
:email => Account::Page::Defaults.email
)
end
end
end
 
def self.down
query = {}
update = {'$unset' => {'pages.email' => true, 'pages.phone' => true, 'pages.callout' => true}}
opts = {'safe' => true}
 
Account.collection.update(query, update, opts) ### <- RAWK
 
end
end

I never liked attributes to begin with ;)

;-) i've made mistakes with $set/$unset methodologies in the past when i wanted to query like : '$exists' => {:root => true} because $exists is always a table scan. in this case, of course, we're just rolling back a schema modification, but i thought i'd mention is since '$unset' often is paired with '$exists' in code.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.