What is the "rails way" to filter an index actions results?

I’m very new to ruby, rails and web dev in general. Been at it for
about 5 weeks.

I’ve been building a site and have come up with a kludgy way to
accomplish most of what I want but I’m sure it’s not the “rails way”
and there are some issues expanding the features so I’d like to know
how y’all do it. 

I have 3 models: users, shifttypes, and shifts and am working on a
site that manages the shifts for a volunteer organization. The tables
and relations are all pretty much working and the site is as well.
there are 1200+ shifts from mid dec to mid april and the index page
has many many pages that a user would have to sift through to find
what they want. It is paginated but I need some filtering

I’ve been able to accomplish this by having a few links at the top of
the page. The link has basically a hard coded parameter (e.g. “?
filterby=current_user”) appened to the route (shifts_path) and in the
controller I test for the parameters and if I see this pattern I use a
named scope on the shift model and the user then sees only their own
shifts. It works well but like I said, it’s kludgy.

I have a couple filters that match this basic patter: unselected
shifts, my shifts, all shifts, etc. I want one that shows shifts for
a given date and I have that working but the user has to manually type
it into the address line themselves unless they want the default
(current day). The current model doesn’t use any forms, just links
and then params attached to the shifts index action basically.

I’ve googled a good bit and haven’t found anything that really helps –
maybe because I am so new but regardless, I just haven’t gotten my
head wrapped around it yet.

I’d really appreciate any help I can get on this.

Ideally, I’d like a section at the top of my page where the user can
select a date from a drop down, select a user name from a drop down,
or some other criteria (checkboxes) to filter the list.

I also want the filter to persist from visit to visit (because each
listed item also has a link that looks like a button which allows the
user to select that shift or clear it if they are already assigned
among other operations if the current user is an administrator etc).
The current method I’ve figured out actually works great in practice
except for the date filter which is a mess.

I hope this makes sense and look forward to any suggestions.



Hi Max,

I think this railscast might be helpful for you -
Also, take a look on has_scope plugin -
https://github.com/plataformatec/has_scope - it could help you to make
controllers skinny.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs