I want to build dynamics filters with rails for one of my model but i don't know the best way to do that.
My model is en Event witch has a Continent, a Country and a Month.
So i want to be able to find an event with several filters. For example, "Found all events in Europe for the Month of Febrary"
What is the best way to do that. I have to make a function findByfilter in my Event model?
Thank you for answer, example are welcome!
(I use rails 3)
I'm guessing what you're looking for is a little beyond just using scope
to create filters:
i.e.
class Event < ActiveRecord::Base
scope :by_continent, lambda {|continent| where(:continent => continent)}
scope :by_country, lambda {|country| where(:country => country)}
...
end
and then doing Event.by_continent('NA').by_country('Canada')
If you're looking to build a more formal search model with a mechanism to to_s
and describe your search, I'd probably start with something like Railscast 111. He builds an advanced search model, which basically lets you set fields then filter down based on what's set.
From that, you could implement your own to_s
method (or another name if you prefer) that would take a look at which search attributes have been set, and build a nice string to describe it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With