Associations Confusion

Doing @emresa.usiario doesn't work for new records, I think.

Try doing @usiario = @emresa.usiario.new, and <% fields_for :usuario,
@usuario do |usr|%>.

Not sure, though, but try it.

Forgot it was a has_many, go @usiario = @empresa.usiarios.new (plural
of usiario)

Hi Marcelo.

You should be using the build method provided by has_many instead of
new. That way the usuario-empresa relationship is created (the
empresa_id is assigned). Using new just creates a new usuario. The line
should look like this

@usuario = @empresa.usuarios.build

If you want to fill the usuario with the form data you could do

@usuario = @empresa.usuarios.build(params[:usuario])

In any case, using the build method will make AR save the usuario when
you save the empresa, so there will be no need to do usuario.save

Regards

Marcelo Junior escribió: