I am building a controller concern, and inside it I need a reference to the current controller's related model. So, if I have something like:
class UsersController < ApplicationController
include Concern
end
module Concern
extend ActiveSupport::Concern
def bla
self.model # ??
end
end
I would like in bla
to get a reference of the current model, so that when I include Concern
in UserController
, I get a User
reference.
Is this possible in Rails?
You can do this, but only if you have followed Convention over Configuration for naming the controller and model
controller_name.classify.constantize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With