Reputation: 387
I am trying to create an array that has multiple internal fields so that at the time of rendering I can save the fields of each one, something like this:
@profile = [{module:"user", Description:"module of users"},{module:"products", Description:"module of products"}]
to render and create records this way:
@profile.each do |prof|
Record.create(module: prof.module, Description: prof.descripcion)
end
but I get this error:
NoMethodError (undefined method `module' for {:module=>"users", :description=>"module of users"}:Hash):
app/controllers/usuarios_controller.rb:31:in `block in busqueda_usuario_perfil'
app/controllers/usuarios_controller.rb:30:in `each'
app/controllers/usuarios_controller.rb:30:in `busqueda_usuario_perfil'
Upvotes: 0
Views: 272
Reputation: 30453
It's a hash and keys are symbols, so you need to use h[:s]
:
@profile.each do |prof|
Record.create(module: prof[:module], description: prof[:descripcion])
end
But as the keys are equal you could do better:
@profile.each do |prof|
Record.create(prof)
end
And I would use lowercase for the keys.
Upvotes: 2
Reputation: 130
To access to hash use square brackets instead of dot.
Try use this:
@profile.each do |prof|
Record.create(module: prof['module'], Description: prof['Description'])
end
Upvotes: 1