简体   繁体   中英

searchlogic - searching for attr_accessor

I have a table symbols which contains columns: id and symbol_name , where id is the primary key of that table

In my Symbol model, I have

class Symbol < ActiveRecord::Base

def symbol_id
"EMI:#{self.id}"
end

end

On my index.html.erb page, users can search for symbols with id 777 by entering "EMI:777" in the search textfield.

I am using searchlogic for searching.

<% form_for @search do |f| %>
    Search:
    <%= f.text_field :symbol_id_or_symbol_name_like %>
    <%= f.submit "Search" %>
<% end %>

When i use 'symbol_id' in the search form , i get the following error message:

The condition 'symbol_id' is not a valid condition, we could not find any scopes that match this.

Any suggestion is most appreciated

According to this post it looks like you will want to do a scope based on the virtual attribute:

class Symbol < ActiveRecord::Base
  attr_accessor :symbol_id
  scope :symbol_id_search, where("symbol_id = ?", symbol_id)  

  def initialize
    @symbol_id = "EMI:#{self.id}"
  end

end

I not sure that the scope is correct so you may need to dig into that a bit based on your model, controller, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM