与搜查trought阵列搜索表
问题描述:
我有一个表称为key_words
至极已一列称为name
与搜查trought阵列搜索表
然后,我有另一个表称为publications
与被叫key_words
至极它是array
类型的柱。
当我创建发布保存在key_words
值是这样的["1", "29", "40"]
(这些是保存在key_words
表IDS)
我如何才能找到一个发布,如果我在搜索input
computers
在这一刻我有一个search_form_for
由出版物找到title
和description
= search_form_for @search, url: publications_path do |f|
#custom-search-input
.input-group.col-md-12
= f.input :title_or_short_description_or_description_cont, input_html: { class: 'form-control input-sm' }, placeholder: 'Buscar', label: false
感谢您的帮助
答
我会创建第三个表key_words_publications
来存储关系而不是数组列。查看HABTM here的文档。
class Publication
has_and_belongs_to_many :key_words
end
class KeyWord
has_and_belongs_to_many :publications
end
那么你应该能够做这样的事情: key_words_name_cont