title: Class variable in Concern Rails 4
body: <p>I created a concern to encapsulate the logic to upload images for some resources of my application, let's say for example that there's an image associated with each course, and the same for every user.</p>
  
  <pre><code>module Picturable
      extend ActiveSupport::Concern
      included do
        PATH_IMAGE = File.join Rails.root,"public","#{table_name}_imgs"
        after_save :save_image
      end
    def photo=(file)
      unless file.blank?
        @file = file
        self.extension = file.original_filename.split(".").last.downcase
      end
    end
    def path_image
      File.join PATH_IMAGE, "#{self.id}.#{self.extension}"
    end
    def path_img
      "/#{self.class.table_name}_imgs/#{self.id}.#{self.extension}"
    end
  
    def has_image?
      File.exists? path_image
    end
  
    private
    def save_image
      unless @file.nil?
          FileUtils.mkdir_p PATH_IMAGE
  
          File.open(path_image, "wb") do |f|
  
              f.write(@file.read)
          end
          @file =nil
      end
    end
  end
  </code></pre>
  
  <p>I have edited the code because some of the methods were in spanish, the concern works as expected but the problem is with the <strong>table_name</strong> variable, I haven't been able to understand how the value changes, sometimes it gets the value of users, sometimes the value of courses, but of course sometimes I get errors because the framework search the image of users in the folder of courses and vice versa.</p>
  
  <p>The way I'm including the Concern looks as follow:</p>
  
  <pre><code>class Course &lt; ActiveRecord::Base
    include Picturable
  end
  </code></pre>
  
  <p>I want to include the concern in each model that is going to have in image associated, but I need to save the images in different folders representing each one of the resources, let's say that users images should be saved in users_imgs folder, and courses's images should be saved in courses_imgs and so on.</p>
  
  <p>Any clue in what am I doing wrong or if my approach is the wrong one.</p>
  
  <p>Here is the error explained with the rails console:
  <img src="https://i.stack.imgur.com/OFYpt.jpg" alt="rails console session"></p>
  
  <p>Thanks</p>