Rails join model not effectively working in my complex application -


i have order, product , in between join model booking.

it has peculiarities:

  • the booking join model doesn't need product model still present. (so bookings can stay on order though product has been deleted.) done by: belongs_to :product, optional: true
  • all 3 models in 1 view: store.
  • the bookings rendered a-synchronically.

the crux of problem seems order has_many bookings relationship seems not working thought would.

been having issues weeks need bottom of it. how did build it?

migrations

# timestamp_create_products.rb class createproducts < activerecord::migration[5.0]   def change     create_table :products |t|       t.string :name       t.text :description       t.references :category, index: true, foreign_key: true        t.timestamps     end   end end  # timestamp_bookings_orders.rb class createbookings < activerecord::migration[5.0]   def change     create_table :bookings |t|       t.belongs_to :order, index: true, foreign_key: true       t.belongs_to :product, index: true, foreign_key: true       t.string :product_name       t.integer :product_quantity        t.timestamps     end      add_monetize :bookings, :product_price   end end  # timestamp_create_orders.rb class createorders < activerecord::migration[5.0]   def change     create_table :orders |t|       t.column :status, :integer       t.timestamps     end      add_monetize :orders, :total   end end 

booking.rb

class booking < applicationrecord   # money: product_price   monetize :product_price_cents   # associaties   belongs_to :product, optional: true   belongs_to :order   # validations   validates :product_quantity, presence: true, numericality: { only_integer: true, greater_than: 0 }   validates :order, presence: true   validates_uniqueness_of :order_id #, scope: :product_id   # actions   after_save     oder.sum_all_bookings   end    def total     product_price * product_quantity   end end 

order.rb

class order < applicationrecord   # money: total   monetize :total_cents   # statuses   enum status: { open: 0, paid: 1, stored: 2, sent: 3, problem: 4 }   # associations   belongs_to :customer, class_name: 'user'   accepts_nested_attributes_for :customer   has_many :bookings, dependent: :destroy    def sum_all_bookings     bookings = self.bookings     sum = 0     bookings.each |booking|       sum += booking.product_quantity * booking.product_price_cents     end     self.total_cents = sum   end    def self.sum_of_all_orders     sum = order.sum(:total_cents)     money.new(sum)   end end 

product.rb

class product < applicationrecord   # money: price   monetize :price_cents, :allow_nil => true, :numericality => { :greater_than_or_equal_to => 0 }   # associaties   belongs_to :category   has_many :orders, through: :bookings   has_many :bookings, dependent: :nullify   has_many :pictures, as: :imageable, dependent: :destroy   # validaties   validates :name, presence: true, length: { maximum: 100 }   validates :description, presence: true, length: { maximum: 500 } end 

bookings_controller.rb

class bookingscontroller < applicationcontroller   before_action :set_order, only: [:create, :update, :destroy]   before_action :set_booking, only: [:update, :destroy]    def create     product = product.find(id: params[:booking][:product_id])     unless @order.bookings.find_by(product_id: product.id)       booking = booking.new(booking_params,                             :product_name  => @product.name,                              :product_price => @product.price )       booking.save       @order.bookings.create(booking: booking)     else        booking = @order.bookings.find_by(product_id: product.id)       booking.product_quantity = params[:booking][:product_quantity]       booking.save     end     @order.save end 

i have tried many different version of building bookings_controller. think mistake lays somewhere else.

any ideas or possibly solution?


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -