Skip to content

Instantly share code, notes, and snippets.

@ferromir
Last active August 29, 2015 14:17
Show Gist options
  • Save ferromir/1ee4a071b719f9aa0bac to your computer and use it in GitHub Desktop.
Save ferromir/1ee4a071b719f9aa0bac to your computer and use it in GitHub Desktop.
ACR - Ruby - Mass asignment
# http://code.tutsplus.com/tutorials/mass-assignment-rails-and-you--net-31695
# Given the mass asignment:
attrs = {:first => "John", :last => "Doe", :email => "[email protected]"}
user = User.new(attrs)
user.first #=> "John"
user.last #=> "Doe"
user.email #=> "[email protected]"
# We add can_fire_missiles...
class AddCanFireMissilesFlagToUsers < ActiveRecord::Migration
def change
add_column :users, :can_fire_missiles, :boolean, :default => false
end
end
# Bad
class User < ActiveRecord::Base
end
# Good
class User < ActiveRecord::Base
attr_accessible :first, :last, :email
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment