Meta_Search undefined method error

1k views Asked by At

Intro: I am working on a Car dealer application so I have a table named cars with fields carname_id and carmodel_id. carname_id and carmodel_id are taken from 2 tables named carnames and carmodels where I keep all car names and car models. Now I've setup these models so I can have them in a relation, something like belongs_to and has_many (not relevant cause this is working as it should be - I can add cars and models to a new car from a select list)

I am using meta_search to search by many fields and the code in the search_form looks like his

<%= f.label :make_contains, "Select Car Make" %>
<%= f.select :make_contains, Car::CAR_MAKE %>

<%= f.label :model_contains, "Select Model" %>
<%= f.text_field :model_contains, Car::CAR_MODEL %>

the make and model fields are the old fileds in the cars table that I used for storing the car and model name of a car. But when I get in to the advanced search form I found myself stuck in choosing dynamicaly the model list.. I have in car.rb a list of car names specified for Car list and a list for car models.. I agree that this is not the best way to do it, that's why I had to create 2 tables: carnames and carmodels that I've told you about at the start.. I have setup everything and the add new car works as expected, I can select a carname_id from carnames table and carmodel_id from carmodels table and it is stored in database perfectly using a form like this:

<%= f.label :carname_id, "Car name:" %>
<%= f.select :carname_id, @select_carnames %>

<%= f.label :carmodel_id, "Model name:" %>
<%= f.select :carmodel_id, @select_carmodels %>

this way I give up on make and model fields from cars table as I have 2 new working fields carname_id and carmodel_id

Now to The Problem: how can I make the search by carmodel_id and carname_id, when making a form like this one:

<%= f.label :carname_id_contains, "Model name:" %>
<%= f.select :carname_id_contains, @select_carnames %>
<%= f.label :carmodel_id_contains, "Model name:" %>
<%= f.select :carmodel_id_contains, @select_carmodels %>

returns me an error
undefined method `carname_id_contains' for MetaSearch::Searches::Car:0xb584b7a4

cars controller:

def index
  @search = Car.search(params[:search])
  @cars = @search.all.paginate :page => params[:page], :per_page => 18
  @select_carnames = Carname.find(:all).map{|element| [element.name, element.id]}
  @select_carmodels = Carmodel.find(:all).map{|element| [element.name, element.id]}

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cars }
      format.json { render :json => @cars.map(&:attributes) }
    end
  end



   def new
    @car = Car.new
    @select_carnames = Carname.find(:all).map{|element| [element.name, element.id]}
    @select_carmodels = Carmodel.find(:all).map{|element| [element.name, element.id]}
        5.times { @car.assets.build }

        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @car }
        end

  end

def edit
  @car = Car.find(params[:id])}
  @select_carnames = Carname.find(:all).map{|element| [element.name, element.id]}}
  @select_carmodels = Carmodel.find(:all).map{|element| [element.name, element.id]}}<br />
  5.times { @car.assets.build }
end

I hope I described it as good as possible and I really would apreciate any help and any ideas on this one. Thanks all for your time.

1

There are 1 answers

0
rmagnum2002 On BEST ANSWER

and the answer is

<%= f.label :carname_id_equals, "Model name:" %>
<%= f.collection_select :carname_id_equals, Carname.order('name ASC').all,  :id, :name, :include_blank => true %><br />

<%= f.label :carmodel_id_equals, "Model name:" %>
<%= f.collection_select :carmodel_id_equals, Carmodel.order('name ASC').all, :id, :name, :include_blank => true %><br />

I'll need to read more the Rdoc in the future :)