简体   繁体   English

在 Rails 3 中使用 MetaSearch 按关联搜索

[英]Search by association in Rails 3 with MetaSearch

I'm using MetaSearch gem in my Rails 3 project.我在我的 Rails 3 项目中使用 MetaSearch gem。

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:我在 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.但是,我没有城市的名称和 ID,而是使用“City:0x00000102a20488”之类的内容,并且搜索无法正常工作。

I think that the mistake is here: ":city_id_equals".我认为错误在这里:“:city_id_equals”。 How to make it correct?如何使它正确?

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

Not sure your question is really clear.不确定你的问题是否真的很清楚。

First of all, I'm guessing you have something like <City:0x00000102a20488> , which is the string representation of a ruby object.首先,我猜你有类似<City:0x00000102a20488>的东西,它是 ruby object 的字符串表示形式。 If you want to display the name of the city, city.name should make the trick (assuming you have a name member on the city.).如果您想显示城市的名称, city.name应该可以解决问题(假设您在城市中有 name 成员。)。

For the search, I don't really get what you are trying to do.对于搜索,我真的不明白你想要做什么。 What is :city_id_equals supposed to mean to you? :city_id_equals对你意味着什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM