Topic: search for tables with many to many relations

I have two tables
1. am_applications
and
2. am_servers

One application can reside on multiple servers and one server can have multiple applications.

I capture the relationship between the two from the am_applications new.rhtml and edit.rhtml.
Here is the sample code of my am_application controller

# am_application controller
def new
    @am_application = AmApplication.new
    @all_servers = AmServer.find(:all, :order=>"server_name")
    @selected_servers = []
   end

  def create
   
    @am_application = AmApplication.new(params[:am_application])
    set_relations
    if @am_application.save
      if request.xml_http_request?
        render :action => 'new_result', :layout => false
      else
        list
        render :action =>  'list'
      end
    else
       render :action => 'new_error', :layout => false
    end
  end

              
  def edit
    @am_application = AmApplication.find(params[:id])
    find_all_relations
  end

  def update
  @am_application = AmApplication.find(params[:id])
  set_relations
  if @am_application.update_attributes(params[:am_application])
      flash[:notice] = 'AmApplication was successfully updated.'
      redirect_to :action => 'show', :id => @am_application
    else
      render :action => 'edit'
    end
  end

  def destroy
    AmApplication.find(params[:id]).destroy
   
    redirect_to :action => 'list'
  end
  def find_all_relations
    @all_servers = AmServer.find(:all, :order=>"server_name")
    @selected_servers = @am_application.am_servers.collect { |cat| cat.id.to_i }
  end      
  def set_relations
       @am_application.am_servers = AmServer.find(params[:am_server_ids]) if params[:am_server_ids]
   
  end


I want to give my user the following ability.

If they need to search for an application by application name that is not a problem but the user wants to search all applications residing on a server.

My join table is am_applications_am_servers.

Any idea how can I accomplish that?

Re: search for tables with many to many relations

  def set_relations
       @am_application.am_servers = AmServer.find(params[:am_server_ids]) if params[:am_server_ids]
  end

I'm curious, can you tell me what the set_relations is doing.  In laymens terms please?
Thanks,
Al
who's having a similar problem which i posted in an earlier email that hasn't received any response yet.

Last edited by alanpieroway (2006-11-15 17:22:10)

Re: search for tables with many to many relations

You can see the applications for any given server like this:

@server.applications

You can also search them like this:

@server.applications.find(:all, :conditions => ['name = ?', params[:name]])

Does that answer your question?

Railscasts - Free Ruby on Rails Screencasts

Re: search for tables with many to many relations

ryanb wrote:

You can see the applications for any given server like this:

@server.applications

You can also search them like this:

@server.applications.find(:all, :conditions => ['name = ?', params[:name]])

Does that answer your question?

What is being assumed here? Is :name a search pattern for applications ?

How would I be able to search applications starting with 'payroll' residing on Server names starting with  'Linux'?

Re: search for tables with many to many relations

That's a little tricker. Try:

@applications = Application.find(:all, :include => :servers, :conditions => "applications.name LIKE 'payroll%' AND servers.name LIKE 'Linux%'")

Last edited by ryanb (2006-11-15 19:09:27)

Railscasts - Free Ruby on Rails Screencasts