1
votes

Why would this not be working?

class Customer < ActiveRecord::Base
    has_many :notes, :class_name => "CustomerNote", :foreign_key => 'customer_id'

    def self.called_this_month
        self.notes.where(:date => Date.today.beginning_of_month..Date.today.end_of_month).count
    end
end

I get this error:

undefined method `notes` for #<Class:0x00000004b37190>

Note Model:

class CustomerNote < ActiveRecord::Base
  self.table_name = "customer_contact"

  belongs_to :customer
end
3
Updated the original post - andy
Should this: self.called_this_month really be this: called_this_month - Sam Peacey
Doing so renders this error: undefined method 'called_this_month' - andy
notes is going to be on an instance of Customer where you're defining called_this_month on the Customer class itself - Sam Peacey

3 Answers

0
votes

You can't called self.notes in a class method as it read as Customer.notes where as you want 'Customer.new.notes'.

so you have to do something like following

change def self.called_this_month to def called_this_month

OR

self.notes.where(:date => Date.today.beginning_of_month..Date.today.end_of_month).count

to

Customer.first.notes.where(:date => Date.today.beginning_of_month..Date.today.end_of_month).count
0
votes

notes is not a class method, it is an instance method. If you need a scope on notes, put it in the Notes model and use something like

def self.called_this_month
  where(:id => Notes.called_this_month.pluck(:customer_id).uniq)
end
0
votes

Model:

class Customer < ActiveRecord::Base
     def self.called_this_month(customer)
             customer.notes.where(:date => Date.today.beginning_of_month..Date.today.end_of_month).count
     end
 end

Controller:

@customer = Customer.find(params[:customer_id])

//this will give you the count of the method call as output.
@count  = Customer.called_this_month(@customer)