ruby on rails 4 - Filtering records results in undefined method when category_id is provided -


i've got problem filtering function in app. please enter here: http://leszczyna.wzks.uj.edu.pl/12_stolarski/events_for_seniors/pl/events?utf8=%e2%9c%93&search=warszawa&date=02-06-2015

search works fine, when try filter categories (left panel) error:

undefined method `category_id'

what more, if change category_id in link category_whatever, error disappear (but filtering still doesn't work). does know why error appears?

here code:

category model

class category < activerecord::base   has_many :events end 

event model

class event < activerecord::base   include filterable   belongs_to :city   belongs_to :category end 

events controller (index action)

def index   if params[:search] && params[:date] && @city     date = params[:date].to_date     @events = @city.events.filter(         params.slice(             :category_id,              :start_date,              :cost)     )     @categories = get_categories(@events)   end end 

filter funtcion (from http://www.justinweiss.com/blog/2014/02/17/search-and-filter-rails-models-without-bloating-your-controller/):

module filterable extend activesupport::concern  module classmethods   def filter(filtering_params)     results = self.where(nil)     filtering_params.each |key, value|       results = results.public_send(key, value) if value.present?     end     results   end end end 

categories panel

<div class="panel-body"> <ul>   <% @categories.each |category| %>       <li><%= link_to category.name, events_path(                               :search => params[:search],                               :date => params[:date],                               :category_id => category.id                       )       %></li>   <% end %> </ul> </div> 

your search tries:

@city.events.category_id 

and doenst work. have same result other parameter cost. goal use this:

@city.events.where(category_id: category_id) 

the filter doenst work relation.

best

edit:

one way solve:

class city < activerecord::base   has_many :events     def filter(key,value)                                                              where("#{key}=?",value)                                                        end   end    def filtered_events params                                                         result = events                                                  sliced_params(params).each |key, value|                                         result = result.filter(key,value)                                              end                                                                              result                                                                         end                                                                             private                                                                           def sliced_params params                                                           params.slice(:category_id, :start_date, :cost)                                 end  end 

and can call

@events = @city.filtered_events(params) 

Comments