我正在尝试将 meta_search 应用于一种表单,其中在所有属性中,一个是按作者姓名进行搜索。它作为两个属性“first_name”和“last_name”存储在表authors 中。
我在 Author 模型中创建了以下虚拟属性:
search_methods :name
def name
self.first_name + " " + self.last_name
end
Authors 和 Books 模型(我正在搜索的模型)的关联如下:
class Book < ActiveRecord::Base
has_many :authors
..
class Author < ActiveRecord::Base
belongs_to :book
..
现在,当我尝试将以下输入粘贴到视图中时,我收到错误:
<%= f.text_field :authors_name_contains, :placeholder => "author.." %>
但如果我通过视图中的“or”运算符将条件应用于属性“first_name”和“last_name”,它就会起作用。
我做错了什么?如何让meta_search使用方法“name”?
I'm trying to apply the meta_search to a form where among all the attributes the one is a search by the author's name. It's stored as the two attributes "first_name" and the "last_name" in the table authors.
I've created the following virtual attribute in the Author model:
search_methods :name
def name
self.first_name + " " + self.last_name
end
The Authors and the Books model, which is the one I'm searching in, are associated like this:
class Book < ActiveRecord::Base
has_many :authors
..
class Author < ActiveRecord::Base
belongs_to :book
..
Now when I'm trying to paste the following input in the view I get the error:
<%= f.text_field :authors_name_contains, :placeholder => "author.." %>
But it works if I apply the conditions to the attributes "first_name" and "last_name" through the "or" operator here in the view.
What am I doing wrong? How to make the meta_search use the method "name"?
发布评论
评论(1)
我认为你做不到。如果我正确理解meta_search,它会查看由ActiveRecord(或您的后端是什么)生成的属性,因此它不会知道有关您的虚拟属性的任何信息。
I don't think you can do that. If I understand meta_search correctly, it's looking at attributes generated by ActiveRecord (or whatever your back end is) so it wouldn't know anything about your virtual attributes.