ruby-on-railsrubyrubygems

Notifications_count problem with the noticed gem


"I'm currently using the noticed gem for notifications, and I'm encountering the following problem:

undefined method `notifications_count=' for #<AppointmentNotifier id: nil, type: "AppointmentNotifier", record_type: "Appointment", record_id: 15, params: {
=>"new Message"}, created_at: nil, updated_at: nil>. 

I can't find the error in any file, and I'm not calling that method. The error just occurred suddenly."

I searched in all the files for any method that might interfere, but I couldn't find anything. I also used the console to look for the error


Solution

  • When you see it returns

    undefined method 'xxxxxx'
    

    on an ActiveRecord object.

    1. First, you want to make sure it's not a typo.
    2. if it's not a typo and you don't define this method, it probably a column's name because ActiveRecord use meta programming to "generate" all methods for columns
    3. notifications_count looks like a column belonging to gem noticed so I thought it's related to data migration

    Conclusion: noticed adds some additional columns to the table and you need to run rails db:migrate to add those new columns first.