Skip to content

Determines which attributes in a given model can be set via mass-assignment.

License

Notifications You must be signed in to change notification settings

nickhoffman/accessible-model-attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

Determines which model attributes in a Hash of parameters can be set via mass-assignment.

Example

Say we have this model:

class User < ActiveRecord::Base
  attr_accessible :username, password
end

And these parameters for creating a new User:

params = {
  :username         => 'bob123',
  :password         => 'secret',
  :hashed_password  => 'd6c1fbcf028923cc0480fff4ab459a21',
  }

If that “params” Hash was used to create a new User:

@user = User.new params

an exception would be raised, because the ‘hashed_password’ attribute is not settable via mass-assignment.

The solution is to extract the “accessible” attributes from the “params” Hash, like this:

accessible_params = AccessibleModelAttributes.extract User, params
@user = User.new accessible_params

The “accessible_params” variable will contain this:

{:username => 'bob123', password => 'secret'}

License

Copyright © 2008 Nick Hoffman, released under the MIT license.

About

Determines which attributes in a given model can be set via mass-assignment.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages