Has_and_belongs_to_many relationship in nested forms

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

On Wed, Mar 25, 2009 at 2:38 AM, Josh M. [email protected]
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 D.
http://destiney.com/

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

<% form_for(@stress_test) do |f| %>

<%= f.label :name %> <%= f.text_field :name %>
<%= f.label :max_concurrent_scans %> <%= f.text_field :max_concurrent_scans %>
<%= f.label :number_of_runs %> <%= f.text_field :number_of_runs %>

<% f.fields_for :tst_definitions do |test_form| %>


  • <%= 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’
    %>


<%= link_to ‘Add a project’, ‘#add_project’, :class =>
‘add_codesecure_project’ %>
<% end -%>
<%= 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();

});

});

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!

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