0

I got a array of strings, I want to retrieve for each the attribute during the creation of the post.

My array = ["_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

The log print > 'MATURITY : :_651_maturity'

I'm looking to the value of :_651_maturity who is a attribute of my post.

I tried .to_sym but it's not working..

Thanks for the help!

6
  • Why are you retrieving all the tables and their columns if you just need one of them (columns[2]). Do you have a model for that table? Commented Jun 4, 2019 at 18:19
  • columns return a object with { nil, nil, { all the columns} } so in fact, I'm getting all the columns with columns[2] Commented Jun 4, 2019 at 18:21
  • Still, do you need a particular table or all of them? Commented Jun 4, 2019 at 18:35
  • All of them. The problem is I can't get the values inside the loop because they are strings and not the relation with the post created .. The log print > ":_651_maturity" and I'm looking to the value of :_651_maturitywho is a attribute of my post. Commented Jun 4, 2019 at 18:41
  • I still don't get it. When you say 'my post' which table do you refer to? Is there a model for it? Commented Jun 4, 2019 at 18:57

1 Answer 1

1

Inside calculate_scoring you can use self to point to the record you are saving. So self._651_maturity = <some_value>, self[:_651_maturity] = <some_value> and self['_651_maturity'] are all valid methods to set _651_maturity.

Also, you can do something like:

my_attrib = '_651_maturity'
self[my_attrib] = 'foo'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.