我得到了一个字符串数组,我想在创建帖子的过程中为每个属性检索。
我的数组 = ["_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
who 是我帖子的一个属性的价值。
我试过了,.to_sym
但它不起作用..
谢谢您的帮助!
在里面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'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句