Search code examples
undefinedactiveadminproduction-environment

Active_Admin Undefined Method Error in Production Only


I've got a User model and a Strengths model. A user has_many strengths, and I'm trying to build an admin panel in Active_Admin for Strengths that displays the User's first_name and email. It works perfectly in development but when I push to Heroku I get this message:

ActionView::Template::Error (undefined method `first_name' for nil:NilClass):
app/admin/strengths.rb:5:in `block (3 levels) in <top (required)>'

This is what I have so far that works in development:

app > admin > strengths.rb
ActiveAdmin.register Strength do
  index do
    column "Strength ID", :id
    column "Name" do |i|
        i.user.first_name
    end
    column "Email" do |i|
        i.user.email
    end
    column :producer
    column :versatility
        default_actions
  end
end

Any ideas why I would get an error in production but not in development?


Solution

  • this may cause becoz u dont have record in Strengths model,

    try adding if condition in first_name and last_name column as,

    column "Name" do |i|
        i.user.first_name if i.user.present? 
    end
    

    this will avoid error if record is not present.