# Many to many self join. Data insertion using text box

**URL:** <https://www.ruby-forum.com/t/many-to-many-self-join-data-insertion-using-text-box/91115>\
**Category:** Rails\
**Created:** [April 4, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/many-to-many-self-join-data-insertion-using-text-box/91115 "2007-04-04T21:05:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vishwaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e19adc/32.png) [@Vishwaraj](https://www.ruby-forum.com/u/Vishwaraj)\
**Post date:** [April 4, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/many-to-many-self-join-data-insertion-using-text-box/91115/1 "2007-04-04T21:05:53Z")

</div>

Hi again,

Well I’m struck at place since several days.

I have a relationship such that many services could be dependent on a  
service. I have listed all the dependent services of a service on the  
show page. Now just below the list of dependents I have provided a text  
field with auto complete and a button so that a user can add new  
dependents to the service. My problem is that I’m unable to read the  
value from the text field and insert into MTM table.

To make things clearer I’ve following table structure for the self join:

services  
id  
.  
.  
service\_dependencies  
service\_id  
dependent\_service\_id  
impact  
severity

I’ve following Model

service.rb  
has\_many :service\_dependencies  
has\_many :dependents, :through =\> :service\_dependencies

service\_dependency.rb  
belongs\_to :service  
belongs\_to :dependent, :class\_name =\> ‘Service’, :foreign\_key =\>  
‘dependent\_service\_id’

Before controller I would tell you what I’m trying to do in my show  
view. Apart from displaying all the fields in services table  
correspoding to the service being displayed currently I have displayed  
all the dependents in following manner  
\<% for depend in @service.dependents %\>

\<%= link\_to depend.send(“service\_name”), :action =\> ‘show’, :id =\>  
depend.id %\>

\<% dependency = @service.service\_dependencies.find(:first, :conditions  
=\>  
[“dependent\_service\_id = ?”, depend.id])%\>  
\<%=h dependency.send(“impact”) %  
  
 \<%=h dependency.send(“severity”) %\>

\<%end%\>

In order to let users’ add more dependents I’m providing text box with  
autocomplete in following manner:

Dependent Service Name  
\<%= text\_field\_with\_auto\_complete 'service\_dependents', 'service\_name', {:size =\> 20}, { :after\_update\_element =\> 'auto\_complete\_on\_select' }%\>

Now I want to create a button Add Dependent and pass the value of the  
service that is displayed at present and the value of the new dependent  
service that the user had entered in the text box above. Something like  
this

\<%= button\_to "Add Dependent", :action =\> 'add\_dependent', :id =\> @service, :param =\> ?? %\>

In the controller I have written the body of add\_dependent in following  
manner:

def add\_dependent  
serv = @service.find(params[:id])  
depend = @service.find(params[:param])  
@service\_dependency = ServiceDependency.new(:service =\> serv,  
:dependent =\> depend)  
if @service\_dependency.save  
flash[:notice] = ‘Dependent added successfully.’  
redirect\_to :action =\> ‘show’, :id =\> serv  
else  
render :action =\> ‘show’, :id =\> serv  
end  
end

If you are still reading, you must have understood my problem. My  
problem is, how would I refer to the value of dependent service entered  
in the text box and pass it to the add\_dependent method?

Similarly, how would I get the values of impact and severity in text  
boxes and pass it to my add\_dependent method.

Thanks in anticipation.
