简体   繁体   English

在attr_accessor上没有方法错误

[英]Getting no method error on attr_accessor

Model.rb 模型.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

Trying to fetch the value array by using the object.DecoderArray.valv in rails console but getting the no method error. 尝试通过在Rails控制台中使用object.DecoderArray.valv来获取值数组,但遇到no方法错误。 Am i using the attr_accessor incorrectly. 我使用了attr_accessor吗?

Whenever you edit a file and you have the rails console opened, you need to restart to apply the changes to the console. 每当您编辑文件并打开rails console ,都需要重新启动以将更改应用到控制台。 To restart, you can use the method Rails::ConsoleMethods#reload! 要重新启动,可以使用方法Rails::ConsoleMethods#reload! .

You can do it simply running reload! 您只需运行reload!就可以做到reload! in rails console . rails console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM