RAILS Seed File Runs but Does Not Save Data in Database

Asked

Viewed 89 times

0

I’m creating a file Seeds for popular my database, this file is responsible for popular Grupos(model :grupo) that wheel without problem and also popular TipoAtividade(model :tipo_atividade), who belongs to the group.
classe ruby Grupo classe ruby TipoAtividade

When I spin the rake db:seed it runs normally and generates the groups and appears to have generated the types as well, just below the code of the file Seeds. classe ruby Seed

But when I look at the database that’s what comes up. tabela tipo de atividade

It creates a row in the database, but does not save the data in the name and reference columns of grupo(grupo_id). I would like to know what is happening, because I have already researched on the Internet and found nothing that was very clear about this problem. I came to see something of "protected_attributes", but Gem conflicts with certain versions of Rails and/or bundler, I could not identify.

If the question is not clear just ask that I edit.

  • If the answer below has worked. You should mark it as "solved".

1 answer

1

In the example above you created 3 groups.
The Group class has a relationship has_many :tipo_atividades

You must have the instance of a Grupo example:

# pegando o útimo grupo criado
grupo3 = Grupo.last
grupo3.tipo_atividades.create(nome: 'Palestra')

or else

# pegando os últimos 3 grupos criados
Grupo.last(3).each do |grupo|
  grupo.tipo_atividades.create(nome: 'Palestra')
end

Using the has_many method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.