vendredi 5 mai 2017

Rails how to automatically associate the objects in the model?

I'm trying to associate the order obtecj with pdata object in the creation of the order object and after the creation of the pdata oject.

but after the creation the order did not get the pdata_id

someone know how do to this?

class Order < ActiveRecord::Base

  belongs_to :product
  belongs_to :pdata

  after_create :create_pdata

  def create_pdata 
    or_id = order.id
    pr_id = product.id

    data = Pdata.find_by_id(pr_id)
    if data.nil?
      attrs = product.attributes
      attrs.delete('created_at')
      attrs.delete('updated_at')
      data = Pdata.create(attrs)

      data = data.or_id

      data.save
    end
  end
end

Aucun commentaire:

Enregistrer un commentaire