ruby on rails - Method errors during before_save -


i'm sure missing simple, have been racking brain past few days on this.

i have booking , review table, booking has many reviews. can create review, run through error when trying define roles of user leaving , receiving review.

here models. review

class review < activerecord::base   before_save :define_review_role   after_create :call_update_rating    belongs_to :booking    belongs_to :client, class_name: "user", primary_key: "client_id"   belongs_to :talent, class_name: "user", primary_key: "talent_id"    def define_review_role     if review_sender_id === self.booking.client_id       review_receiver_id = self.booking.talent_id     else       review_receiver_id = self.booking.client_id     end     self.update   end    def call_update_rating     user = user.find(self.review_receiver_id)     if review_receiver_id == self.booking.talent_id       user.update_talent_rating(self.rating)     else       user.update_client_rating(self.rating)     end     user.save   end end 

and booking model

class booking < activerecord::base   # start validations   validates :amount, format: {with: /(\d{1,3})(\.\d{1,2})?/, :message => "field invalid. please enter correct amount."}    belongs_to :user    belongs_to :client, class_name: "user", primary_key: "client_id"   belongs_to :talent, class_name: "user", primary_key: "talent_id"    has_many :reviews    has_many :sent_reviews, class_name: "review", primary_key: "talent_id"   has_many :received_reviews, class_name: "review", primary_key: "client_id"    def client     user.find(client_id)   end    def talent     user.find(talent_id)   end end 

i have been able create review fine, , upon inspection finding review.review_receiver_id being left blank. define_review_role reason not running, have tried before_create, after_save, after_create , no dice.

i know not running because upon inspection, review_receiver_id being left blank.

i able access information through review.booking.talent_id, , review.booking.client_id, connections there. know must missing have no idea what.

your method define_review_role running have badly written code. should this

def define_review_role   if review_sender_id === self.booking.client_id     self.review_receiver_id = self.booking.talent_id   else     self.review_receiver_id = self.booking.client_id   end end 

if try assign value without self value assigned newly created local method instead of attribute of report class.

you cannot call save or update on end of method because calling before_save callback. methods save , update trigger again , method gonna called again , create infinity loop.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -