1

I am working on a Rails 5 application, and have a number of methods that apply across multiple models.

I was thinking about putting them in:

lib/model_extension.rb

and then, in every model that uses these methods, I would add:

include ModelExtension

Would you recommend doing it differently, for Rails 5.1?

EastsideDev
  • 6,257
  • 9
  • 59
  • 116

2 Answers2

1

Create an ActiveSupport::Concern and place it in app/models/concerns. This is the conventional approach since Rails 4+ http://api.rubyonrails.org/v5.1/classes/ActiveSupport/Concern.html

You don't have to follow this convention. Refer to this issue for some good discussion: How to use concerns in Rails 4

Sean Huber
  • 3,945
  • 2
  • 26
  • 31
0

This is what concerns are for. They should go in app/models/concerns, which rails new creates for you.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • as in app/models/concerns/model_extension.rb, or app/models/conerns.rb and if it's the latter, I don't have to explicitly include it in the model where I want it used? – EastsideDev Oct 23 '17 at 23:11
  • `app/models/concerns` is a directory that will already exist in your Rails 5.1 app. – user229044 Oct 23 '17 at 23:12