Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get model for current controller

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?

like image 419
linkyndy Avatar asked Jan 08 '23 14:01

linkyndy


1 Answers

You can do this, but only if you have followed Convention over Configuration for naming the controller and model

controller_name.classify.constantize
like image 143
Rajdeep Singh Avatar answered Jan 19 '23 07:01

Rajdeep Singh