TJ Sherrill
TJ Sherrill

Reputation: 2645

why is this class method throwing an error: Attempt to call private method `create'

I am getting an error running the following method in an after_save callback

NoMethodError in ApprovalsController#update Attempt to call private method `create'

 def create_next_approval
      self.recommendation.create :email => self.next_approver_email #if next_approver_email? && recently_approved?
 end

This method is giving me fits in general. I am trying to create a new object after_save using an attribute: next_approver_email.

I also had to comment out the second part of the method because it was returning a no method error: next_approver_email?

I thought I could pass a ? to a method and it would check it?

Upvotes: 0

Views: 332

Answers (2)

Taryn East
Taryn East

Reputation: 27747

If your model has been defined as has_many :recommendations then you have to use the plural form of the word eg:

self.recommendations.create :email => self.next_approver_email

As to checking the email - as the commenter mentioned, you can use empty?, or (better yet) present? eg:

self.recommendations.create :email => self.next_approver_email if next_approver_email.present? && recently_approved.present?

Upvotes: 1

Andrew Kuklewicz
Andrew Kuklewicz

Reputation: 10701

Hard to tell exactly, but here is a theory:

If you have a has_one relation for recommendation, you should be calling create_recommendation, not recommendation.create:

self.create_recommendation :email=>self.next_approver_email #if next_approver_email? && recently_approved?

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one

Upvotes: 1

Related Questions