Reputation: 13771
I have an Opportunity
model and its attributes include:
derated_sales
est_full_value
est_workshare
p_win
I am trying to calculate the following:
derated_sales = est_full_value.to_f * (est_workshare.to_i/100).to_f * (p_win.to_i/100).to_f
Here is the code for my model:
class Opportunity < ActiveRecord::Base
after_save :calculate_derated_value
def calculate_derated_value
opportunity.derated_sales = est_full_value.to_f * (est_workshare.to_i/100).to_f * (p_win.to_i/100).to_f
end
end
However, after I click "create", the "derated_sales" I get the error "undefined local variable or method `opportunity'"
Please help. Thanks!
Upvotes: 0
Views: 16
Reputation: 29369
You should be saying self.derated_sales
. Since calculate_derated_value
is call on the instance of Opportunity, anything you call will be called on the object.
class Opportunity < ActiveRecord::Base
before_save :calculate_derated_value
def calculate_derated_value
self.derated_sales = self.est_full_value.to_f * (self.est_workshare.to_i/100).to_f * (self.p_win.to_i/100).to_f
end
end
Also, if you want to save the value to the database, you should be setting it in before_save
instead of after_save
. after_save
happens after the record gets created in the database.
Upvotes: 1