Skip to content

Instantly share code, notes, and snippets.

@azuby
Created February 23, 2012 09:41
Show Gist options
  • Select an option

  • Save azuby/1891965 to your computer and use it in GitHub Desktop.

Select an option

Save azuby/1891965 to your computer and use it in GitHub Desktop.
# Permissions
permission = []
[
{ action: "create", resource: "User", access: "none" },
{ action: "read", resource: "User", access: "individual" },
{ action: "update", resource: "User", access: "individual" },
{ action: "destroy", resource: "User", access: "individual" },
{ action: "create", resource: "Shift", access: "none" },
{ action: "read", resource: "Shift", access: "individual" },
{ action: "update", resource: "Shift", access: "individual" },
{ action: "destroy", resource: "Shift", access: "individual" },
{ action: "create", resource: "Job", access: "none" },
{ action: "read", resource: "Job", access: "company" },
{ action: "update", resource: "Job", access: "company" },
{ action: "destroy", resource: "Job", access: "company" }
].each_with_index do |params, i|
permission[i] = Permission.where(params).first_or_create!
end
# Roles
[
{ name: "Administrator", permission_ids: [2, 6, 8, 9, 10, 11, 12] },
{ name: "Supervisor", permission_ids: [2, 6, 10] },
{ name: "Basic", permission_ids: [6] }
].each do |params|
role = Role.where( name: params[:name] ).first_or_create!
params[:permission_ids].each do |permission_id|
puts permission[permission_id].inspect #this works
role.privileges.create!( permission_id: permission[permission_id].object_id ) #this throws the error below
end
end
# Error: gems/activerecord-3.2.0/lib/active_record/validations.rb:56:in `save!': Validation failed: Permission can't be blank (ActiveRecord::RecordInvalid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment