0

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

lyuba
  • 6,250
  • 7
  • 27
  • 37

1 Answers1

0

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.

jdc
  • 733
  • 3
  • 7
  • As far as it's stated in the docs it looks like this is possible with the use of the scopes: https://github.com/dogenpunk/meta_search However I can't make it work. Usage of the scopes was also suggested here (for the searchlogic though): http://stackoverflow.com/questions/2801096/searchlogic-and-virtual-attributes – lyuba Jul 07 '11 at 14:21
  • I'm currently using this: `<%= f.search_field :authors_first_name_or_authors_last_name_contains, :placeholder => "author.." %> while it seems to bulky to have in the view. – lyuba Jul 07 '11 at 14:23
  • 1
    Maybe you need to create a scope on your model named authors_name_contains? – jdc Jul 07 '11 at 15:19