Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added attr accessible and protected examples.
- Loading branch information
1 parent
ad83986
commit 5226a79
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__)) | ||
require 'mongo_mapper' | ||
|
||
MongoMapper.database = 'testing' | ||
|
||
class User | ||
include MongoMapper::Document | ||
key :email, String | ||
key :admin, Boolean, :default => false | ||
|
||
# Only accessible or protected can be used, they cannot be used together | ||
attr_accessible :email | ||
end | ||
|
||
# only accessible are set on new/create/etc. | ||
user = User.create(:email => 'IDontLowerCaseThings@gmail.com', :admin => true) | ||
puts user.admin # false | ||
|
||
# can be set using accessor | ||
user.admin = true | ||
user.save | ||
puts user.admin # true |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__)) | ||
require 'mongo_mapper' | ||
|
||
MongoMapper.database = 'testing' | ||
|
||
class User | ||
include MongoMapper::Document | ||
key :email, String | ||
key :admin, Boolean, :default => false | ||
|
||
# Only accessible or protected can be used, they cannot be used together | ||
attr_protected :admin | ||
end | ||
|
||
# protected are ignored on new/create/etc. | ||
user = User.create(:email => 'IDontLowerCaseThings@gmail.com', :admin => true) | ||
puts user.admin # false | ||
|
||
# can be set using accessor | ||
user.admin = true | ||
user.save | ||
puts user.admin # true |