0

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'

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

no longer shows me the error but when saving the records the fields are null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.