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| %> <table> <tr> <td><label for="empresa_razao_social">Razo social:</label></td> <td><%= emp.text_field :razao_social %></td> </tr> (...) <% 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"!!! :[ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
up -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-18 15:45 UTC
Re: 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. On Jan 18, 3:51 pm, Marcelo Junior <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> up > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 Junior <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-18 16:03 UTC
Re: Associations Confusion
Forgot it was a has_many, go @usiario = @empresa.usiarios.new (plural of usiario) On Jan 18, 4:57 pm, Marcelo Junior <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> augustlille...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 Junior <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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? > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Forgot it was a has_many, go @usiario = @empresa.usiarios.new (plural > of usiario) > > On Jan 18, 4:57 pm, Marcelo Junior <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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 ????????? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marcelo Junior wrote:> augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> Forgot it was a has_many, go @usiario = @empresa.usiarios.new (plural >> of usiario) >> >> On Jan 18, 4:57 pm, Marcelo Junior <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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ó:> > Marcelo Junior wrote: >> augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >>> Forgot it was a has_many, go @usiario = @empresa.usiarios.new (plural >>> of usiario) >>> >>> On Jan 18, 4:57 pm, Marcelo Junior <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> >> 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? >-- Andrés Cirugeda Esco 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---