def capitalize_first_words
[:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each
do
|key|
self.key = key.gsub(/^(\W*?[a-z])/) { |m| m.upcase }
if !key.blank?
end
end
def capitalize_first_words
[:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each
do
|key|
self.key = key.gsub(/^(\W*?[a-z])/) { |m| m.upcase }
if !key.blank?
end
end
self.key = key.to_s.gsub(/^(\W*?[a-z])/) { |m| m.upcase } if
!key.to_s.blank?
end
end
Note the difference betweet self.key and key.
self.key calls method key on self.
key - is block local variable, which is instance of Symbol ( :question,
:correct_ans_1 …).
Symbol has not methods ‘.gsub’ and ‘.blank?’, so it must be converted to
String to call ‘.gsub’ and ‘.blank?’
Instance of Question doesn’t have attribute “key”.
If this is ActiveRecord, you maybe should make migration “add_column
:questions, :key, :string”, OR within class scope write “attr_accessor
:key”
Instance of Question doesn’t have attribute “key”.
If this is ActiveRecord, you maybe should make migration “add_column
:questions, :key, :string”, OR within class scope write “attr_accessor
:key”
I thought key was a variable containing the attribute. The instance of
Question should have the attribute contained in the variable… It just
doesn’t seem to be evaluating key.
Not sure of either. But your instinct is correct, key isn’t being
evaluated before it is passed to the self reference, so you’re still
looking for a key method that isn’t there.
end
end
key is still not being interpreted as “question” or “correct_ans_1”,
ect…
Could it be a scope issue? I am in the Question model.
Walter
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.