daspianist
daspianist

Reputation: 5525

Heroku undefined method error in ActionView

Heroku logs shows the following error:

ActionView::Template::Error (undefined method `accepted_answer_id' for #<Question:0x000000052fd1e0>)

As background, I have a model Question, which has_many Answers. Question also has a column accepted_answer_id.

The undefined method error is referring a line in _question.html.erb, where its used to customized the display of the answer count in a different shaded background

  <% if question.answers.count > 0 %> 

    <% if question.accepted_answer_id %>
        <div class="pickedanswer">
          <h4><%= question.answers.count %></h4>
        </div>
    <% else %>
        <div class="numanswer">
          <h4><%= question.answers.count %></h4>
        </div>
    <% end %>

  <% else %>

      <div class="noanswer">
        <h4><%= question.answers.count%></h4>
      </div>

  <% end %>

I think the error may be raised due to the fact that initially question.accepted_answer_id is nil.. but given the way I structured the logic I do not know why it couldn't simply follow along (as it has done successfully in development).

Thanks for your help.

Upvotes: 1

Views: 1615

Answers (2)

ChrisBarthol
ChrisBarthol

Reputation: 4959

I've come across this problem multiple times and these are the steps I take:

  1. First make sure you have migrated the database on heroku:

    heroku run rake db:migrate
    
  2. Make sure the database was actually migrated on heroku:

    heroku pg:psql -a app_name
    \d table_name
    
  3. Finally restart the app on heroku:

    heroku restart
    

Upvotes: 2

catsby
catsby

Reputation: 11352

Your schema is missing that attribute. If your code works locally, then either you added that column manually, or a migration is not in your version control.

You can see what migrations have run like so:

$ heroku run rake db:migrate:status --app app_name

This will show if any of your migrations haven't been ran. You should match those to your local migrations. Are you maybe missing one?

Running db:push will destroy your remote database and replace it with a local copy, at best. Generally you should use pgbackups:restore to do this kind of thing (https://devcenter.heroku.com/articles/heroku-postgres-import-export)

Upvotes: 2

Related Questions