【发布时间】:2018-02-13 09:21:41
【问题描述】:
模型.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。
【问题讨论】:
-
假设您的问题中的
object是BaseTable的一个实例......您当然应该打电话给object.valv吗?此外,您的方法名称应以小写字符开头。类名大写。最后,以您实现DecoderArray的方式,它已经为您返回@valv -
哦,伙计,我只需要重新启动控制台...我不知道为什么它不起作用。感谢您的帮助。
标签: ruby-on-rails attr