0

I create a simple module in ruby to implement on ActiveRecord model.

module Parameterize
  def to_param
    "#{ id } #{ name }".parameterize
  end
end

The question it's where to place the file?

  • app/models
  • config/initializers
  • lib
  • other
Javier Valencia
  • 697
  • 7
  • 24
  • 2
    lib : Extended modules for your application. – pangpang Apr 27 '15 at 06:59
  • If you put your module in **lib**, it will be accessible everywhere and you may just have to include them inside any Class – Abhi Apr 27 '15 at 07:01
  • 'lib' is not the standard practice anymore. concerns is generally considered the place to put stuff that will be included in Active Records – Taryn East Apr 27 '15 at 07:02
  • [__The Rails Concern are used to DRYING the code for both models and controllers.__](https://signalvnoise.com/posts/3372-put-chubby-models-on-a-diet-with-concerns) it is more about repeated code. For me `lib/my_module` good choose. – Roman Kiselenko Apr 27 '15 at 07:06
  • So what happens when you discover that you need it in a second model? you move it from lib to concerns? what if I stop using a concern in a second model - do I now need to move it to the lib directory? what if I'm looking or where I find the module that I'm including in model X? I have to look in "either concerns or lib" depending on whether or not it's included in just this model or in this model and other models?" better to just keep "things you include in models" in the one place... whether it's included in one or multiple... ? – Taryn East Apr 27 '15 at 07:14

1 Answers1

3

These generally are placed in a new directory off app called concerns - it's worth googling for more info about how that works.

here's a good ref of places to go look: How to use concerns in Rails 4

Community
  • 1
  • 1
Taryn East
  • 27,486
  • 9
  • 86
  • 108