K
K
Konstantin T2015-03-07 08:53:42
Ruby on Rails
Konstantin T, 2015-03-07 08:53:42

How to pass parameter to Concern for model in ROR?

There is, for example, a certain function in concern that should take a parameter from the model in which it was connected, how best to do this?
For example:
concern:

module Fields
  extend ActiveSupport::Concern

  def set_fields
    self.fields(variable_from_model).each do |f|
      # some code
    end
  end

Model:
class User < ActiveRecord::Base
  include ::Fields
  variable_from_model= 'user'
end

Here's how to pass such a variable to concern in order to make the correct fetch from the database, i.e. each model inserts its value into variable_from_model

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin T, 2015-03-07
@RooTooZ

Everything turned out to be easy!

module Fields
  extend ActiveSupport::Concern

  included do
    class_attribute :entity
  end

  def set_fields
    self.fields(self.class.entity).each do |f|
      # some code
    end
  end

class User < ActiveRecord::Base
  include ::Fields
  self.entity = 1
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question