Reputation: 1219
I'm trying to loop over a the attr_accessor-able variables in an ActiveModel::Model
and then fetch corresponding records from the db based on them, the problem is that it's not finding the record.
class DeployOptionForm
include ActiveModel::Model
attr_accessor :revision_number
end
And this is the code I was trying to get them.
dof.instance_variables.each do |var_sym|
dof_var = dof.instance_variable_get(var_sym)
var_sym.to_s.slice!(0).to_sym
doe = DeployOption.new
doe.value = dof_var
doe.deploy_option_type = DeployOptionType.find_by name: var_sym
doe.deploy = @deploy
doe.save
end
I know that if I do
doe.deploy_option_type = DeployOptionType.find_by name: :revision_number
the record is found just fine but I'm trying to make it more adaptable.
So what do I need to do to var_sym
to be able to do DeployOptionType.find_by name: var_sym
?
Upvotes: 0
Views: 75
Reputation: 1219
Solved it!
Ended up having to do this
var_sym = var_sym.to_s
var_sym.slice!(0)
var_sym = var_sym.to_sym
EDIT: Actually this is a better solution and does it in 1 line!
var_sym = var_sym.to_s.slice(1..-1).to_sym
Upvotes: 1
Reputation: 239491
var_sym.to_s.slice!(0).to_sym
isn't doing anything. var_sym
will be exactly what it was before you did all that.
You need var_sym = var_sym.to_s.slice(0).to_sym
Upvotes: 1