开发者

Traversing the models with meta_search and referring to the virtual attributes

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"?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜