Associations Confusion

Guys,very well:

I have the table Empresas:

id: int
(…)

and table Usuarios:

id: int
empresa_id: int
(…)

In models, I makining the follow:

  1. class Empresa < ActiveRecord::Base
  2. has_many :usuario

  1. class Usuario < ActiveRecord::Base
  2. belongs_to :empresa

In partial form, I makeing thus:

<% form_for :empresa, @empresa, :url => {:action => ‘create’} do |emp|
%>

(…)

<% fields_for :usuario, @empresa.usuario do |usr|%>
<table>
  <tr>
    <td>Nome:</td>
    <td><%= usr.text_field :nome %></td>
  </tr>

(…)

That return the follow error:

undefined method `nome’ for Usuario:Class

However, if I to invert the associations in models, for example:

  1. class Empresa < ActiveRecord::Base
  2. belongs_to:usuario

  1. class Usuario < ActiveRecord::Base
  2. has_many :empresa

In this way it functioned correct, now my question. I have a “empresa”
with many “usuarios”(has_many ok?), and many “usuario” belogs of one
“empresa”(belongs_to ok?). Why the logic it is contrary? Or I make a
mistake?

And more, want I save “empresa”, not save “usuario”, only “empresas”!!!
:[

Razão social: <%= emp.text_field :razao_social %>

up

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.

On Jan 18, 3:51 pm, Marcelo J. [email protected]

[email protected] wrote:

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.

On Jan 18, 3:51 pm, Marcelo J. [email protected]

Well, I make:

(…)
@empresa = Empresa.new(params[:empresa])
@usuario = @empresa.usuario.new
if @empresa.save and @usuario.save
(…)

and
(…)
<% fields_for :usuario, @usuario do |usr|%>
(…)

But appear the follow message when save:
undefined method `usuario’ for #Empresa:0x6705aac

Why?

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

On Jan 18, 4:57 pm, Marcelo J. [email protected]

[email protected] wrote:

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

On Jan 18, 4:57 pm, Marcelo J. [email protected]

Well, now save only “empresa”, but not “usuario”.

I make action thus:

def create
@empresa = Empresa.new(params[:empresa])
@usuario = @empresa.usuarios.new
if @empresa.save and @usuario.save
rendirect_to :controller => ‘login’, :action => ‘index’
else
render :action => ‘new’
end
end

???

Marcelo J. wrote:

[email protected] wrote:

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

On Jan 18, 4:57 pm, Marcelo J. [email protected]

Well, now save only “empresa”, but not “usuario”.

I make action thus:

def create
@empresa = Empresa.new(params[:empresa])
@usuario = @empresa.usuarios.new
if @empresa.save and @usuario.save
rendirect_to :controller => ‘login’, :action => ‘index’
else
render :action => ‘new’
end
end

???

it forgives me!!! Not was saving because of “validates”. The “empresa”
and “usuario” now they are saving, but de field “empresa_id” of
“usuario” it is null. How make for save empresa id? @usuario.empresa_id
= @empresa.id? Not have make this using only associations?

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 J.
escribió:>

???

it forgives me!!! Not was saving because of “validates”. The “empresa”
and “usuario” now they are saving, but de field “empresa_id” of
“usuario” it is null. How make for save empresa id? @usuario.empresa_id
= @empresa.id? Not have make this using only associations?


Andrés Cirugeda E.
ASPgems
Email: andres at aspgems dot com

‘All we have to decide is what to do with the time that is given to us’.
Gandalf.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs