Rails 3 manually creating model and persisting not working -


so have model object needs insert packing slips if model saved (the model in question payment).

i attempted in after_save hook payment model, never persisted packing slips. moved controller in if @payment.save blah blah block, still not persist models. code below:

  if @payment.save      if @payment.order.has_physical_product?       # generate packing slip shipping       slip = packingslip.new(:payment_id => @payment.id, :department => "shipping")       slip.save!        if @payment.order.has_book?         slip = packingslip.new(:payment_id => @payment.id, :department => "royalty")         slip.save!       end      end       membershipmailer.membership_email(@order) unless !@order.has_membership? 

note membershipmailer firing know it's in there, packing slips not persist. attempt replicate functionality hand in console , works fine. not sure stopping it. have no validations in packingslip model @ moment.

when it's not persisting, mean association isn't there, or it's not being saved in database?

one option (as brian mentioned above) add debug logging see what's going on. i've taken liberty of refactoring code more rails-like (assuming payment has_many :packing_slips):

class payment < activerecord::base   has_many :packing_slips   after_save :generate_packing_slips    def generate_packing_slips     if order.has_physical_product?       packing_slips.create(:department => "shipping")       packing_slips.create(:department => "royalty") if order.has_book?     end      # @ point, packing_slips collection should      # populated - valid or not, can check what's going on.     # if you're not getting output here, packing slips     # aren't being generated, means there's problem     # order.has_physical_product?      if rails.env.development?       packing_slips.each |ps|         rails.logger.debug("error messages: #{ps.errors.full_messages.inspect}") unless ps.valid?       end     end      # btw, `unless !foo` same `if foo`     membershipmailer.membership_email(order) if order.has_membership?   end end 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -