【发布时间】:2021-07-04 04:43:58
【问题描述】:
我目前正在重新构建搜索功能,我们有一个基本搜索和一个高级搜索,我有一个基本搜索表单正在工作,但是,我似乎无法让基于选项的搜索工作。
这是我目前所拥有的。
场景:
我需要能够选择下拉选项,然后输入我的搜索词,这是我的表单的样子
我的表单是这样的
<%= form_tag contacts_path, method: :get do %>
<div class='l-inline-row-block'>
<div class='l-inline-col'>
<%= select_tag(:qs, options_for_select(['name', 'customers', 'suppliers', 'tags'], selected: params[:qs])) %>
</div>
<div class='l-inline-col'>
<%= search_field_tag :search, params[:search] %>
</div>
<div class='l-inline-col'>
<%= submit_tag submit_text, { class: 'no_print' } %>
</div>
</div>
<% end %>
我在控制器索引方法中有以下内容
@contacts = Contact.search(params[:qs], params[:search])
以及模型中的以下内容
SEARCHABLE_FIELDS = [
'name',
'customers_name',
'suppliers_name',
'tags'
]
def self.search(field, query)
if field.present? && query.present? && SEARCHABLE_FIELDS.include?(field)
where(arel_attribute(field).matches("%#{query}%"))
else
all
end
end
这是数据库结构:
create_table "contacts", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "customer_account_id"
t.integer "supplier_account_id"
t.string "name"
t.string "salutation"
t.string "title"
t.string "phone"
t.string "mobile"
t.string "business_email"
t.string "private_email"
t.date "date_of_birth"
t.string "spouse"
t.string "address_1"
t.string "address_2"
t.string "address_3"
t.string "address_4"
t.string "postcode"
t.text "other_information", limit: 65535
t.integer "created_by"
t.integer "updated_by"
t.string "contact_type"
t.integer "assigned_to"
t.datetime "created_at"
t.datetime "updated_at"
t.string "company_name"
t.string "web_address"
t.string "second_phone"
t.integer "prospect_strength"
t.boolean "obsolete"
t.string "url"
t.index ["obsolete"], name: "index_contacts_on_obsolete", using: :btree
end
create_table "accounts", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "short_name", limit: 9
t.string "name"
t.string "type"
t.string "child_type"
t.integer "parent_id"
t.integer "position"
t.datetime "created_at"
t.datetime "updated_at"
t.decimal "value", precision: 11, scale: 2, default: "0.0"
t.boolean "fixed", default: false
t.boolean "allow_new_child", default: false
t.integer "created_by"
t.integer "updated_by"
t.boolean "disabled", default: false
t.boolean "locked", default: false
t.boolean "restricted", default: false
t.string "account_section"
t.string "description"
t.boolean "is_budget_account", default: true
t.boolean "is_budget_enabled", default: false
t.integer "sequence"
t.index ["type"], name: "index_accounts_on_type", using: :btree
end
使用acts_as_taggable 作为需要搜索的标签。
如果有帮助,这是先前搜索使用的当前方法
def quick_search_fields
@quick_search_fields = [
{
col_name: 'name',
title: 'name',
column_names: ['contacts.name']
},
{
col_name: 'customer_name',
title: 'customer',
search_tables: [:customer],
column_names: ['accounts.name']
},
{
col_name: 'supplier_name',
title: 'supplier',
search_tables: [:supplier],
column_names: ['accounts.name']
},
{
col_name: 'tags',
title: 'tags',
tags: true,
tagged: Contact
}
]
end
这是我得到的错误:
Mysql2::Error: Unknown column 'contacts.customers' in 'where clause': SELECT `contacts`.* FROM `contacts` WHERE (`contacts`.`suppliers` LIKE '%john%') ORDER BY id asc LIMIT 20 OFFSET 0
高级搜索选项卡看起来像这样,我尽量不使用 gem,因为我想学习如何最好地做到这一点,显然有很多选项,但我想专注于基于基本选项现在搜索。
谢谢。
【问题讨论】:
标签: ruby forms activerecord ruby-on-rails-5 rails-activerecord