Had this working until I refactored by putting into a hash and checking for blank. Now I get the error: undefined method `key?'' for nil:NilClass. I''m still kind of new at this and would appreciate suggestions on what I am missing... before_validation :capitalize_first_words def capitalize_first_words [:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each do |key| self.key = self.key.gsub(/^(\W*?[a-z])/) { |m| m.upcase } if self.key != blank end end Thanks, Dave -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1abbb33bff62ddb8abba0998a602b469%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Maybe you mean this? before_validation :capitalize_first_words 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 вторник, 3 сентября 2013 г., 15:55:04 UTC+3 пользователь Ruby-Forum.com User написал:> > Had this working until I refactored by putting into a hash and checking > for blank. Now I get the error: undefined method `key?'' for > nil:NilClass. > > I''m still kind of new at this and would appreciate suggestions on what I > am missing... > > > before_validation :capitalize_first_words > > def capitalize_first_words > [:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each > do |key| > self.key = self.key.gsub(/^(\W*?[a-z])/) { |m| m.upcase } if > self.key != blank > end > end > > > Thanks, > > Dave > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d985ca7c-23ea-4dc2-9456-0f3e28587210%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Michael Aleksandrovich wrote in post #1120515:> Maybe you mean this? > > before_validation :capitalize_first_words > > 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 > > , 3 2013 ., 15:55:04 UTC+3 Ruby-Forum.com > User :Thanks! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/49e0fa710d6a925b576c2a0eb4e79a37%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Now giving error: undefined method `key'' for #<Question:0x007fdff26b7798> In Question model: before_validation :capitalize_first_words def capitalize_first_words [:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each do |key| self.key = self.key.gsub(/^(\W*?[a-z])/) { |m| m.upcase } if !self.key.blank? end end Am I missing something really quite simple? Thanks -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7ab2093213f91856ff4da26b60ca355b%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
before_validation :capitalize_first_words def capitalize_first_words [:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each do |key| 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?'' среда, 4 сентября 2013 г., 1:03:10 UTC+3 пользователь Ruby-Forum.com User написал:> > Now giving error: > undefined method `key'' for #<Question:0x007fdff26b7798> > > In Question model: > before_validation :capitalize_first_words > > def capitalize_first_words > [:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each > do |key| > > self.key = self.key.gsub(/^(\W*?[a-z])/) { |m| m.upcase } if > !self.key.blank? > end > end > > Am I missing something really quite simple? > > Thanks > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8bd8a4a0-2626-4fe8-958c-64d8c9939a9e%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
But, even the following causes error: undefined method `key='' for #<Question:0x007fcba5972f20> before_validation :capitalize_first_words def capitalize_first_words [:question, :correct_ans_1, :correct_ans_2].each do|key| self.key = "x" 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1e2a081f66832bf47930d5c31699819f%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On Sep 4, 2013, at 8:17 AM, Dave Castellano wrote:> But, even the following causes error: undefined method `key='' for > #<Question:0x007fcba5972f20> > > before_validation :capitalize_first_words > > def capitalize_first_words > [:question, :correct_ans_1, :correct_ans_2].each do|key| > self.key = "x"maybe self.send(":#{key}=", "x") or: self[key] = "x" 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> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1e2a081f66832bf47930d5c31699819f%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/BCDF2DBA-8948-428E-95D5-9C9C0D66E73A%40wdstudio.com. For more options, visit https://groups.google.com/groups/opt_out.
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" среда, 4 сентября 2013 г., 15:17:47 UTC+3 пользователь Ruby-Forum.com User написал:> > But, even the following causes error: undefined method `key='' for > #<Question:0x007fcba5972f20> > > before_validation :capitalize_first_words > > def capitalize_first_words > [:question, :correct_ans_1, :correct_ans_2].each do|key| > self.key = "x" > 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. > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fa614bd1-6b3c-4e7d-ab18-cfb41b18145c%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Michael Aleksandrovich wrote in post #1120644:> 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" > > , 4 2013 ., 15:17:47 UTC+3 Ruby-Forum.com User > :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. I''m sure i''m missing something simple... Any other possibilities? Dave -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0fcf1758efbcec93855835955bbdf694%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.