rails/rails — Issue #3326: helper method seems to not load record completely before code is executed
Author: salex
Created: 2011-10-14 03:48:12+00:00
State: open

I wrote a series of helper methods that basically produces a formated assessment (test,quiz) from a classic
Assessments has many Questions has many Answers model

I was just testing it by calling the main method <%= format_assessment(@assessment) %> at the end of my show page and all was working well.

I then tried to implement a "test" action in the assessments controller and got an error

```
undefined method `downcase' for nil:NilClass
Extracted source (around line #17):

14:

15:
Section - <%=@assessment.category%>

16: <%=form_tag post_assessment_path(@assessment), :id => "assessarea", :name => "assessarea", :"data-behaviors" => "validateRequired" %>
17: <%= format_assessment(@assessment)%>
18:
19:
20:

Rails.root: /Users/salex/work/take/test/dummy

Application Trace | Framework Trace | Full Trace
activesupport (3.1.1) lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/salex/work/take/app/helpers/take/assessments_helper.rb:28:in `block in make_questions'
activerecord (3.1.1) lib/active_record/associations/collection_proxy.rb:91:in `each'
activerecord (3.1.1) lib/active_record/associations/collection_proxy.rb:91:in `method_missing'
/Users/salex/work/take/app/helpers/take/assessments_helper.rb:26:in `make_questions'
/Users/salex/work/take/app/helpers/take/assessments_helper.rb:8:in `format_assessment'
/Users/salex/work/take/app/views/take/assessments/display.html.erb:17:in `___sers_salex_work_take_app_views_take_assessments_display_html_erb__741771835808616027_2173483800'
actionpack (3.1.1) lib/action_view/template.rb:144:in `block in render'
```

the code for the first couple methods is:

``` ruby
def format_assessment(assmnt)
result = <<-HERE

#{make_instruction(assmnt.instructions)}

#{make_questions(assmnt)}

HERE
return result.html_safe
end

def make_instruction(text)
result = <<-HERE

#{text}

HERE
return result
end

def make_questions(assmnt)
result = ""
assmnt.questions.each do |question|
if question.display.downcase != "none" #### error is on this line
result << make_group_header(question.group_header) unless question.group_header.blank?
end
if question.display.downcase == "inline"
result << make_inline_question(question)
else
result << make_list_question(question)
end
end
return result
end
```

I could not understand why it worked calling it from my show page, but was saying that my question model had a nil column

I added a logger.info question.inspect line right before the error line to see if I could find anything out

``` ruby
def make_questions(assmnt)
result = ""
assmnt.questions.each do |question|
logger.info question.inspect
if question.display.downcase != "none"
result << make_group_header(question.group_header) unless question.group_header.blank?
end
if question.display.downcase == "inline"
result << make_inline_question(question)
else
result << make_list_question(question)
end
end
return result
end
```
The assessment then displayed without an error!!! Removing the logger call, adding junk code, etc would return the error. Only the logger call could get it to work -- and that was sheer luck.

Not sure what else I can send or do to provide you any additional information.