Newbie: where to put input validation and model aggregations

I’m feeling a bit dense and am having a hard time grasping the best way
to implement simple search in Rails. Basically, I just want an input
textbox where a user can type a query which then checks records in the
database … extremely simple. I already wrote a little ‘search’
action in my controller, however I’m trying to stick with the whole
Rails philosophy, and had a few more questions.

-If I want to validate the user input, where should the methods go? As
this is not really model validation, I’m not sure the best place for
them. Should they be protected methods in the controller, or should I
create a whole new class (perhaps something like SearchQueryString) that
handles validation of the query?

-If I only want to display some of the columns of the model in the
output, where is the best place to specify this? Should I override the
find methods in the model or explicitly list them in the controller? Or
should I create a DB view and a new model based on this view? What
about it I want to do something more complex, like have the model always
display my DB “TIMESTAMP” columns as dates or present the results as
simple aggregations … where/how is the best way to specify this:
model, controller, or view?

For example, if I have a table that is something like:
and I would like the output to look like:
“first_seen, last_seen, count, event”
which would be constructed from a SQL query like:
“SELECT min(t) as first_seen, max(t) as last_seen, count(1) as count,
event FROM mytable GROUP BY event”
how should I structure my models? Should I have 2 models, 1 for the raw
and one for the aggregate (based on a DB view) or a single model that
knows how to aggregate the table?

Also, as an aside, given that the display is a table, are there methods
to help out with the sorting of the various columns? What are the “best
practices” involving this sort of thing? I figure its a common idiom


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