Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created February 1, 2012 04:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/1715041 to your computer and use it in GitHub Desktop.
Save ahoward/1715041 to your computer and use it in GitHub Desktop.
removing a bunch of mongo attributes in one hit #L19
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
@tritonrc
Copy link

tritonrc commented Feb 1, 2012

I never liked attributes to begin with ;)

@ahoward
Copy link
Author

ahoward commented Feb 1, 2012

;-) 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment