Forum: Ruby on Rails Many to many self join. Data insertion using text box

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Aebb836b5b2f9a6476d0107d12c84991?d=identicon&s=25 Vishwaraj (Guest)
on 2007-04-04 21:05
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:


I've following Model

has_many :service_dependencies
has_many :dependents, :through => :service_dependencies

belongs_to :service
belongs_to :dependent, :class_name => 'Service', :foreign_key =>

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

<% dependency = @service.service_dependencies.find(:first, :conditions
   ["dependent_service_id = ?",])%>
<%=h dependency.send("impact") %
<br> <%=h dependency.send("severity") %>


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

<p><label for="service_dependents_service_name">Dependent Service
<%= 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

<p><%= button_to "Add Dependent", :action => 'add_dependent', :id =>
@service, :param => ?? %></p>

In the controller I have written the body of add_dependent in following

def add_dependent
  serv = @service.find(params[:id])
  depend = @service.find(params[:param])
  @service_dependency = => serv,
:dependent => depend)
      flash[:notice] = 'Dependent added successfully.'
      redirect_to :action => 'show', :id => serv
      render :action => 'show', :id => serv

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.
This topic is locked and can not be replied to.