Last active
August 29, 2015 14:17
-
-
Save ferromir/1ee4a071b719f9aa0bac to your computer and use it in GitHub Desktop.
ACR - Ruby - Mass asignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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