John Smith
John Smith

Reputation: 6259

Check attributes of model

I wrote a little code to test in wich atrributes of my model Icd3Code there are values that are longer than 255. I do this because i first defined all of my fields in the model as text but not all attributes requiere so much space! So i would like to change some attributes to string:

My code:

arr = Icd3Code.new.attributes.keys

Icd3Code.all.each do |f|
    arr.each do |a|
        if f.a.length >= 255
            puts a 
        end
    end
end

Icd3Code.new.attributes.keys

gives me such an output and thats a problem: ["id", "abrechenbar", "alter_fehler", "alter_o" Because there all strings!

Means that f.a.length returns a error:

method_missing': undefined method `a' for #<Icd3Code:0x7f395f0> (NoMethodError)

How can i fix this? Thanks

Upvotes: 0

Views: 70

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53018

All you have to do is replace f.a.length with a.length.

I think you need to check values rather than keys of attributes. Here is the updated code

Icd3Code.all.each do |f|
    f.attributes.values.each do |a|
        if a.length >= 255
            puts a 
        end
    end
end

Upvotes: 2

Related Questions