I would like to use a single form to display multiple existing associations and to permit additions to those that already exist. In its simplest form a row in U has an association to a row R through a row in C. Now, in my case both R and U must exist before they may be associated and neither may be created in the same process that creates the association row in C. Further, the association in C contains data unique to that association. In the us_controller I can either use @u.r.build or I can use @u.c.build to create an empty new association for ActionView. However, if I go with @u.c.build instead of @u.r.build then I will end up with a nil object from the c.r_id field which will lead to errors in the view. If I choose to use @u.r.build then I will avoid the nil object error but of necessity I will also create new instances of R, which I most definitely do not want to do. I have looked at the railscasts on complex forms and I have purchased and viewed the advanced forms screencasts from the Pragmatic Programmers. I can see what is done there but I cannot seem to map what I have seen to what I want to accomplish. In the form I want to do a dynamic lookup on R by name and use the Iid of the selected R for the C association. But I cannot get the form to display the selectbox for R because for new records, unless an r instance is built along with c then the c.r_id value is null and that is an error. SO, how is this done? I imagine that someone who wants to add products to an order would not wish to allow order entry clerks to add products on the fly, so what is the technique that is used to add multiple associations between a single U and multiple R''s on one form and at the same time does not allow instances of R to be created? Sincerely, -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 18, 4:56 pm, byrnejb <byrn...-fqAF1SpE7daWxEb2tNKyrg@public.gmane.org> wrote:> Now, in my case both R and U must exist before they may be associated > and neither may be created in the same process that creates the > association row in C. Further, the association in C contains data > unique to that association.Hi, I was about to post a very similar problem although I think it is a bit simpler so an answer would be great if someone can give us a hand. What I found in those tutorials about complex forms is that they create relations on the fly to new objects (also created in the same process) and this case is slightly different you need to establish a relation between the object you are instantiating and an existing one. In my case I have a model say House and I need to assign it a Category instance, but I don''t want to instantiate a new Category for that object, I just need to associate that object with an existing category object via its ID, how can I do that? I''ve been looking at fields_for and nested models but I don''t think this is the case to apply that approach, let me know if I''m wrong. Thanks! m. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
On Dec 18, 9:50 pm, Manuca <man...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > What I found in those tutorials about complex forms is that they > create relations on the fly to new objects (also created in the same > process) and this case is slightly different you need to establish a > relation between the object you are instantiating and an existing one. > > In my case I have a model say House and I need to assign it a Category > instance, but I don''t want to instantiate a new Category for that > object, I just need to associate that object with an existing category > object via its ID, how can I do that? I''ve been looking at fields_for > and nested models but I don''t think this is the case to apply that > approach, let me know if I''m wrong. >I answered the cause my own question by tracking down exactly what the nil object was. It turned out to be a misuse of dom_id() on my part. I believe that the way that this problem is handled in Rails is through the Javascript libraries. While it is possible to create both the relation and the target at the same time (as seems canonical given the online examples and references) it is not necessary and appropriate safeguards in the controller will prohibit it. In the view the question is how to reference existing row values in a lookup table. What we need consider is what gets passed back in the params hash. When dealing with the case where the association is the critical element then naturally what we need pass back is a valid foreign key reference. In the case given above that would likely be a value assigned to params[:house][:category_id]. So, in the controller you test that this is != nil on return (i.e in the create or update controller methods). If it is nil then a new Category object is being requested and you need handle that circumstance as appropriate (Raise an error or create the object as desired). In the view you can do the lookup on the reference table several ways to obtain the reference row id. The standard approach is a forms_helper select() that is dynamically populated from the reference table, displays the some meaningful text in the drop down menu, and returns the selection''s associated row id as the value for the foreign_key reference. An alternative is text_field-with_auto_complete (). The main thing to recall is that since we are not initialising a null record for the reference table in the controller, then we must not use any elements of a conjectural row from that table in the view code. In my case I was using such a reference and that was what caused my problem. Hope this helps. It still muddy in my own mind which is why I cannot make my explanation clearer. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.