0

I have a concern looking something like

module StrategyConcern
  extend ActiveSupport::Concern

  included do
    attr_accessor :strategy_display_name
  end
end

and i have included it in my model include StrategyConcern but when i try to assign value to that like this

obj["strategy_display_name"] = "test_display_name"

i am getting error

undefined method `strategy_display_name=' for #<Test:0x00007f868e4aa350>

I want that object attribute to access, without getting the mentioned error.

Ram Hegde
  • 3
  • 3

2 Answers2

0

Given that obj is an instance of the model, you should use

obj.strategy_display_name = "test_display_name"

So just get rid of the square brackets ([] which are mainly used when working with arrays and hashes)

You can read more about accessors there for instance: What is attr_accessor in Ruby?

0

You have to use variables in models using the following way:

obj.strategy_display_name = 'test_display_name'
obj.strategy_display_name

The [] operator for active record models is used to access variables holding column values from the database, not instance variables

Jibran Usman
  • 93
  • 1
  • 5