I receive undefined method 'search_type' for the code below. Can you tell me what am I doing wrong here? Probably something with calling private functions, but I can’t find what the problem is.
class Entry < ActiveRecord::Base
attr_accessible :content, :rank, :title, :url, :user_id
def self.search(params)
t, o = search_type(params[:type]),search_order(params[:order])
scope = self
scope = scope.where(t) if t
scope.order(o).page(params[:page]).per_page(20)
end
private
def search_order(order)
return 'comments_count DESC' if order == '1'
return 'points DESC' if order == '2'
'rank DESC'
end
def search_type(type)
return nil unless type.present?
"entry_type = #{type}"
end
end
In the controller, I have only @entries = Entry.search(params).
It’s not to do with the privateness of your methods, but the fact that
searchis a class method, so when you callsearch_orderfrom within it, it is looking for a class method calledsearch_orderbut you’ve definedsearch_orderas in instance method.Make your 2 helper methods class methods and you should be ok. if you want them to be private class methods, then
If you are wondering why
@entries.search(...)works it’s because I assume that @entries is something likeEntry.where(...)ie, a scope and you can call class methods on scopes.