Search code examples
ruby-on-railsruby-on-rails-3associationsmeta-search

Search by association in Rails 3 with MetaSearch


I'm using MetaSearch gem in my Rails 3 project.

I have two models:

class Company < ActiveRecord::Base
  belongs_to :city
end

class City < ActiveRecord::Base
  has_many :companies
end

I have the action in CompaniesController:

def index
  @search = Company.search(params[:search])
  @companies = @search.all
end

The action's view contains:

= form_for @search do |f|
  = f.label :city_id_equals
  = f.select :city_id_equals
  = f.submit 'Search'

I want a list with city names to be rendered and the opportunity to search the companies by city. But instead of the names and ids of the cities I have something like "City:0x00000102a20488" and the search doesn't work properly.

I think that the mistake is here: ":city_id_equals". How to make it correct?


Solution

  • The solution is found!

    Instead of:

    = f.label :city_id_equals
    = f.select :city_id_equals
    

    I should use:

    = f.label :city_id_equals
    = f.collection_select :city_id_equals, City.all, :id, :name, :include_blank => true