Hi, I have two models (say model a and b) that are joined using the has_and_belongs_to_many association. In model A I want to be able to link to model B in a form. But, when I create a nested_form for model B I have a problem. Instead of linking to the exciting records in Model B. It creates new ones. Is there anyway to use nested_forms so that model B objects are linked to, but not created on submission? When I create a new model A I just want to link to, not create new model Bs. Is this possible? Thanks, Josh --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Greg Donald
2009-Mar-25 15:28 UTC
Re: has_and_belongs_to_many relationship in nested forms
On Wed, Mar 25, 2009 at 2:38 AM, Josh Moore <joshsmoore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have two models (say model a and b) that are joined using the > has_and_belongs_to_many association. In model A I want to be able to link > to model B in a form. But, when I create a nested_form for model B I have a > problem. Instead of linking to the exciting records in Model B. It creates > new ones. Is there anyway to use nested_forms so that model B objects are > linked to, but not created on submission? When I create a new model A I > just want to link to, not create new model Bs. Is this possible?Yes, and it''s likely someone could help debug the issue if you post the code. -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---
Here is the code. I am sorry because this is work I cannot post the entire
application. Thanks for all help and I hope this makes my problem clearer.
Thanks,
Josh
class StressTest < ActiveRecord::Base
has_and_belongs_to_many :tst_definitions
accepts_nested_attributes_for :tst_definitions
has_many :tst_datas
end
class StressTestsController < ApplicationController
layout ''tests.html''
def index
@stress_tests = StressTest.all
end
def new
@stress_test = StressTest.new
#want to add not create new tst_definitions
@stress_test.tst_definitions<< TstDefinition.first
end
def create
@stress_test = StressTest.new(params[:stress_test])
logger.info params.to_yaml
if @stress_test.save
flash[:notice] = "Successfully create Stress Test:
#{@stress_test.name
}"
redirect_to :action => ''index''
else
render :action => ''new''
end
end
def destroy
@stress_test = StressTest.find(params[:id])
@stress_test.destroy
redirect_to :action => ''index''
end
end
The new form
New Stress Test
<br/>
<% form_for(@stress_test) do |f| %>
<table>
<tr>
<td><%= f.label :name %></td><td><%=
f.text_field :name %></td>
</tr>
<tr>
<td><%= f.label :max_concurrent_scans
%></td><td><%= f.text_field
:max_concurrent_scans %></td>
</tr>
<tr>
<td><%= f.label :number_of_runs %></td><td><%=
f.text_field
:number_of_runs %></td>
</td>
</table>
<% f.fields_for :tst_definitions do |test_form| %>
<ul>
<li><%= test_form.select :name,
TstDefinition.find_by_sql("select id,
name from tst_definitions").collect {|test| [test.name, test.id]} %>
<%link_to ''Remove'', ''#remove_project'',
:class => ''remove_codesecure_project''
%></li>
</ul>
<%= link_to ''Add a project'',
''#add_project'', :class =>
''add_codesecure_project'' %>
<% end -%>
<br/>
<%= f.submit ''submit'' %>
<% end -%>
Javascript that addess a new drop down so that multiple tst_definitions can
be added to a single stress_test
$(document).ready(function() {
$("a[class=\"add_codesecure_project\"]").click(function(event){
var copy_object = $("ul").children().eq(0).clone()
copy_object.appendTo("ul");
var num_of_children = $("ul").children().size();
copy_object.children("select").attr("id",
copy_object.children("select").attr("id").replace(/0/,
(num_of_children -
1)));
copy_object.children("select").attr("name",
copy_object.children("select").attr("name").replace(/0/,
(num_of_children -
1)));
//alert(copy_object.children("select").attr("id").replace(/0/,
(num_of_children - 1)));
//alert(copy_object.children("select").attr("id"))
//alert(copy_object.attr("id"));
add_remove_link();
});
});
On Wed, Mar 25, 2009 at 11:28 PM, Greg Donald
<gdonald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> On Wed, Mar 25, 2009 at 2:38 AM, Josh Moore
<joshsmoore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Hi,
> >
> > I have two models (say model a and b) that are joined using the
> > has_and_belongs_to_many association. In model A I want to be able to
> link
> > to model B in a form. But, when I create a nested_form for model B I
> have a
> > problem. Instead of linking to the exciting records in Model B. It
> creates
> > new ones. Is there anyway to use nested_forms so that model B objects
> are
> > linked to, but not created on submission? When I create a new model A
I
> > just want to link to, not create new model Bs. Is this possible?
>
> Yes, and it''s likely someone could help debug the issue if you
post the
> code.
>
>
>
> --
> Greg Donald
> http://destiney.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, After I looked into it some more I figured it out. I had a little bit of a concept mix up with accepts_nested_attributes_for. Because I do not want to create nested record. I do not think it is appropriate for this situation. So I do not use it anymore. Thanks for the help sorry to cause people trouble. Josh On Thu, Mar 26, 2009 at 10:00 AM, Josh Moore <joshsmoore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here is the code. I am sorry because this is work I cannot post the entire > application. Thanks for all help and I hope this makes my problem clearer. > > Thanks, > > Josh > > > class StressTest < ActiveRecord::Base > has_and_belongs_to_many :tst_definitions > accepts_nested_attributes_for :tst_definitions > has_many :tst_datas > end > > > class StressTestsController < ApplicationController > layout ''tests.html'' > def index > @stress_tests = StressTest.all > end > > def new > @stress_test = StressTest.new > > #want to add not create new tst_definitions > @stress_test.tst_definitions<< TstDefinition.first > end > > def create > @stress_test = StressTest.new(params[:stress_test]) > logger.info params.to_yaml > if @stress_test.save > flash[:notice] = "Successfully create Stress Test: #{@ > stress_test.name}" > redirect_to :action => ''index'' > else > render :action => ''new'' > end > end > > def destroy > @stress_test = StressTest.find(params[:id]) > @stress_test.destroy > redirect_to :action => ''index'' > end > end > > > > The new form > New Stress Test > <br/> > > <% form_for(@stress_test) do |f| %> > <table> > <tr> > <td><%= f.label :name %></td><td><%= f.text_field :name %></td> > </tr> > <tr> > <td><%= f.label :max_concurrent_scans %></td><td><%= f.text_field > :max_concurrent_scans %></td> > </tr> > <tr> > <td><%= f.label :number_of_runs %></td><td><%= f.text_field > :number_of_runs %></td> > </td> > </table> > > <% f.fields_for :tst_definitions do |test_form| %> > <ul> > <li><%= test_form.select :name, TstDefinition.find_by_sql("select id, > name from tst_definitions").collect {|test| [test.name, test.id]} %> <%> link_to ''Remove'', ''#remove_project'', :class => ''remove_codesecure_project'' > %></li> > </ul> > <%= link_to ''Add a project'', ''#add_project'', :class => > ''add_codesecure_project'' %> > <% end -%> > > <br/> > <%= f.submit ''submit'' %> > > <% end -%> > > Javascript that addess a new drop down so that multiple tst_definitions can > be added to a single stress_test > $(document).ready(function() { > $("a[class=\"add_codesecure_project\"]").click(function(event){ > var copy_object = $("ul").children().eq(0).clone() > copy_object.appendTo("ul"); > var num_of_children = $("ul").children().size(); > copy_object.children("select").attr("id", > copy_object.children("select").attr("id").replace(/0/, (num_of_children - > 1))); > copy_object.children("select").attr("name", > copy_object.children("select").attr("name").replace(/0/, (num_of_children - > 1))); > //alert(copy_object.children("select").attr("id").replace(/0/, > (num_of_children - 1))); > > //alert(copy_object.children("select").attr("id")) > //alert(copy_object.attr("id")); > > add_remove_link(); > }); > > }); > > > > On Wed, Mar 25, 2009 at 11:28 PM, Greg Donald <gdonald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> On Wed, Mar 25, 2009 at 2:38 AM, Josh Moore <joshsmoore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > Hi, >> > >> > I have two models (say model a and b) that are joined using the >> > has_and_belongs_to_many association. In model A I want to be able to >> link >> > to model B in a form. But, when I create a nested_form for model B I >> have a >> > problem. Instead of linking to the exciting records in Model B. It >> creates >> > new ones. Is there anyway to use nested_forms so that model B objects >> are >> > linked to, but not created on submission? When I create a new model A I >> > just want to link to, not create new model Bs. Is this possible? >> >> Yes, and it''s likely someone could help debug the issue if you post the >> code. >> >> >> >> -- >> Greg Donald >> http://destiney.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 -~----------~----~----~----~------~----~------~--~---
So how would you do something like this? I''m not sure whether I would be better off going with accepts_nested_attributes_for acts_as_taggable. My example is a habtm relationship between books and authors. In a "new book" form, I would love to be able to connect authors already existing in the database, but then being able to add a new author if they don''t already exist. Thanks! On Mar 26, 2:52 am, Josh Moore <joshsmo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, After I looked into it some more I figured it out. I had a little bit > of a concept mix up with accepts_nested_attributes_for. Because I do not > want to create nested record. I do not think it is appropriate for this > situation. So I do not use it anymore. Thanks for the help sorry to cause > people trouble. > > Josh > > On Thu, Mar 26, 2009 at 10:00 AM, Josh Moore <joshsmo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Here is the code. I am sorry because this is work I cannot post the entire > > application. Thanks for all help and I hope this makes my problem clearer. > > > Thanks, > > > Josh > > > class StressTest < ActiveRecord::Base > > has_and_belongs_to_many :tst_definitions > > accepts_nested_attributes_for :tst_definitions > > has_many :tst_datas > > end > > > class StressTestsController < ApplicationController > > layout ''tests.html'' > > def index > > @stress_tests = StressTest.all > > end > > > def new > > @stress_test = StressTest.new > > > #want to add not create new tst_definitions > > @stress_test.tst_definitions<< TstDefinition.first > > end > > > def create > > @stress_test = StressTest.new(params[:stress_test]) > > logger.info params.to_yaml > > if @stress_test.save > > flash[:notice] = "Successfully create Stress Test: #{@ > > stress_test.name}" > > redirect_to :action => ''index'' > > else > > render :action => ''new'' > > end > > end > > > def destroy > > @stress_test = StressTest.find(params[:id]) > > @stress_test.destroy > > redirect_to :action => ''index'' > > end > > end > > > The new form > > New Stress Test > > <br/> > > > <% form_for(@stress_test) do |f| %> > > <table> > > <tr> > > <td><%= f.label :name %></td><td><%= f.text_field :name %></td> > > </tr> > > <tr> > > <td><%= f.label :max_concurrent_scans %></td><td><%= f.text_field > > :max_concurrent_scans %></td> > > </tr> > > <tr> > > <td><%= f.label :number_of_runs %></td><td><%= f.text_field > > :number_of_runs %></td> > > </td> > > </table> > > > <% f.fields_for :tst_definitions do |test_form| %> > > <ul> > > <li><%= test_form.select :name, TstDefinition.find_by_sql("select id, > > name from tst_definitions").collect {|test| [test.name, test.id]} %> <%> > link_to ''Remove'', ''#remove_project'', :class => ''remove_codesecure_project'' > > %></li> > > </ul> > > <%= link_to ''Add a project'', ''#add_project'', :class => > > ''add_codesecure_project'' %> > > <% end -%> > > > <br/> > > <%= f.submit ''submit'' %> > > > <% end -%> > > > Javascript that addess a new drop down so that multiple tst_definitions can > > be added to a single stress_test > > $(document).ready(function() { > > $("a[class=\"add_codesecure_project\"]").click(function(event){ > > var copy_object = $("ul").children().eq(0).clone() > > copy_object.appendTo("ul"); > > var num_of_children = $("ul").children().size(); > > copy_object.children("select").attr("id", > > copy_object.children("select").attr("id").replace(/0/, (num_of_children - > > 1))); > > copy_object.children("select").attr("name", > > copy_object.children("select").attr("name").replace(/0/, (num_of_children - > > 1))); > > //alert(copy_object.children("select").attr("id").replace(/0/, > > (num_of_children - 1))); > > > //alert(copy_object.children("select").attr("id")) > > //alert(copy_object.attr("id")); > > > add_remove_link(); > > }); > > > }); > > > On Wed, Mar 25, 2009 at 11:28 PM, Greg Donald <gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> On Wed, Mar 25, 2009 at 2:38 AM, Josh Moore <joshsmo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > Hi, > > >> > I have two models (say model a and b) that are joined using the > >> > has_and_belongs_to_many association. In model A I want to be able to > >> link > >> > to model B in a form. But, when I create a nested_form for model B I > >> have a > >> > problem. Instead of linking to the exciting records in Model B. It > >> creates > >> > new ones. Is there anyway to use nested_forms so that model B objects > >> are > >> > linked to, but not created on submission? When I create a new model A I > >> > just want to link to, not create new model Bs. Is this possible? > > >> Yes, and it''s likely someone could help debug the issue if you post the > >> code. > > >> -- > >> Greg Donald > >>http://destiney.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 -~----------~----~----~----~------~----~------~--~---