我得到了一个字符串数组,我想在创建帖子时检索每个字符串数组的属性。
我的数组= ["_646_maturity", "_660_maturity", "_651_maturity", "_652_maturity", "_641_maturity"]
class Audit < ApplicationRecord
belongs_to :user
before_save :calculate_scoring
def calculate_scoring
scoring = []
models = ActiveRecord::Base.connection.tables.collect{|t| t.underscore.singularize.camelize.constantize rescue nil}
columns = models.collect{|m| m.column_names rescue nil}
columns[2].each do |c|
if c.include? "maturity"
Rails.logger.debug 'COLUMN : '+c.inspect
scoring.push(c)
end
end
getMaturity = ""
scoring.each do |e|
getMaturity = e.to_sym.inspect
Rails.logger.debug 'MATURITY : '+getMaturity
end
end
end日志打印> 'MATURITY : :_651_maturity'
我正在寻找:_651_maturity的值,它是我的帖子的一个属性。
我试过.to_sym,但它不工作..
谢谢你的帮助!
发布于 2019-06-05 03:18:59
在calculate_scoring中,您可以使用self指向要保存的记录。因此,self._651_maturity = <some_value>、self[:_651_maturity] = <some_value>和self['_651_maturity']都是设置_651_maturity的有效方法。
此外,您还可以执行以下操作:
my_attrib = '_651_maturity'
self[my_attrib] = 'foo'https://stackoverflow.com/questions/56449071
复制相似问题