Forum: Ruby on Rails has_and_belongs_to_many relationship in nested forms

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bf512c651c47ac6421e41f93dc19c726?d=identicon&s=25 Josh Moore (Guest)
on 2009-03-25 10:01
(Received via mailing list)
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
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-03-25 16:29
(Received via mailing list)
On Wed, Mar 25, 2009 at 2:38 AM, Josh Moore <joshsmoore@gmail.com>
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/
Bf512c651c47ac6421e41f93dc19c726?d=identicon&s=25 Josh Moore (Guest)
on 2009-03-26 03:00
(Received via mailing list)
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();
  });

});
Bf512c651c47ac6421e41f93dc19c726?d=identicon&s=25 Josh Moore (Guest)
on 2009-03-26 07:53
(Received via mailing list)
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
5e6ceef905d14ade228ea22c445d57bc?d=identicon&s=25 Aidan F (Guest)
on 2009-03-26 10:09
(Received via mailing list)
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!
This topic is locked and can not be replied to.