attr_accessor 上没有方法错误
Getting no method error on attr_accessor
Model.rb
class BaseTable < ApplicationRecord
before_save :ConverterArray
serialize :conF_string
attr_accessor :valv
def initialize(valv)
@valv = valv
end
# def self.Conf_count # self. is used when u have to call the method on te whole class example User.Conf_count
# Confirmer.count
# end
def ConverterArray # self. is not used when u have to call the method on the instance variable for example user = User.new, user.ConverterArray
count = Confirmer.count
index = Array.new(count, 0)
conf = conF_string.map(&:to_i)
conf.each do |n|
index[n] = 1
end
index = index*""
self.conF_string = index
end
def DecoderArray
count = Confirmer.count
value = []
conf = conF_string.split("")
i = 0
conf.each_with_index do |n,index|
if n=="1"
value[i] = index
i+=1
end
end
@valv = value
end
end
尝试使用 rails 控制台中的 object.DecoderArray.valv 获取值数组,但出现无方法错误。我是不是用错了attr_accessor。
每当您编辑文件并打开 rails console
时,您需要重新启动才能将更改应用到控制台。重启可以使用方法Rails::ConsoleMethods#reload!
.
您可以在 rails console
.
中简单地 运行 reload!
Model.rb
class BaseTable < ApplicationRecord
before_save :ConverterArray
serialize :conF_string
attr_accessor :valv
def initialize(valv)
@valv = valv
end
# def self.Conf_count # self. is used when u have to call the method on te whole class example User.Conf_count
# Confirmer.count
# end
def ConverterArray # self. is not used when u have to call the method on the instance variable for example user = User.new, user.ConverterArray
count = Confirmer.count
index = Array.new(count, 0)
conf = conF_string.map(&:to_i)
conf.each do |n|
index[n] = 1
end
index = index*""
self.conF_string = index
end
def DecoderArray
count = Confirmer.count
value = []
conf = conF_string.split("")
i = 0
conf.each_with_index do |n,index|
if n=="1"
value[i] = index
i+=1
end
end
@valv = value
end
end
尝试使用 rails 控制台中的 object.DecoderArray.valv 获取值数组,但出现无方法错误。我是不是用错了attr_accessor。
每当您编辑文件并打开 rails console
时,您需要重新启动才能将更改应用到控制台。重启可以使用方法Rails::ConsoleMethods#reload!
.
您可以在 rails console
.
reload!