Topic: has_and_belongs_to_many and searching.

I am trying to develop a search engine. I have the following models:

class Technology < ActiveRecord::Base
acts_as_ferret :store_class_name => true
has_one :economic
has_and_belongs_to_many :diseases
validates_uniqueness_of :name
end

class Disease < ActiveRecord::Base
acts_as_ferret :store_class_name => true
has_and_belongs_to_many :technologies
validates_uniqueness_of :name
end

Basically, I want to create a search that will list technologies.

I have a search controller/view:

def search
sorting = Ferret::Search::SortField.new(:name)
@query = params[:query]
@results = Technology.find_by_contents(@query.to_s, {:limit => :all, :sort => sorting})
render :action => 'display_results'
end

I want to have a query that lists all of the technologies in a given disease area.

In my controller when I create a technology, I have an array as follows:

@technology.diseases = Disease.find(@params[:disease_ids]) if @params[:disease_ids]

I'm wondering how I can access a list of diseases by name for a technology?

I want to ultimately be able to type this in the search:

diseases: Coronary Heart Disease

It will return all of the technologies that are used for treating Coronary Heart Disease.

I hope that makes sense.

Re: has_and_belongs_to_many and searching.

Hi Jordan,

I'm not sure I understand the question, but here goes. To find all technologies for a specific disease by name:

@technologies = Disease.find_by_name("Corronary Heart Disease").technologies

Then, in your view, you could iterate over all of the technologies. Is that what you were looking for?