Where to put a function called by a model?

I’m using a slight modification of the simple_date_select plugin to
display and accept input for dates in m/d/yyyy format. But this doesn’t
handle the view of dates when they are not being edited.

Being new to Ruby and Rails I couldn’t figure out how to create a
similar simple_date_view plugin. And it seems too much to ask that
someone else create one! So the next best thing is to at least create a
function that my models can call for each date field. I tried putting
this function in controllers/application.rb but nothing seems to happen,
not even an error message. So I suspect that functions in that location
are not available to models. So where would I put my little
date-conversion function?

By the way, I can get the code to work if it’s in the model itself, but
I want to have a global function so I don’t have to repeat the code for
each field.



Any classes/modules you add to your app/model or lib directories are
accessible from any of your models (and your controllers and views).
So you could add a class like this:

Class DateToolbox
def self.convert_date(d)


Then from any model you can call DateToolbox.convert_date.

A slightly more complicated but cleaner approach would be to override
or augment the simple_date_select plugin with your conversion
routine. Here is a simple example of adding a method to Ruby’s built-
in String class:

class String
def hello
“hello #{self.to_s}”

“aaron”.hello => hello aaron


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