Closed bachpx195 closed 3 years ago
/model.rb dependent: Destroy xóa phần tử sẽ xóa luôn trong các bảng khác phụ thuộc vào nó.
rails50/depot_f/app/models/cart.rb
class Cart < ApplicationRecord
**has_many :line_items, dependent: :destroy**
end
rails50/depot_f/app/models/product.rb class Product < ApplicationRecord ➤ has_many :line_items ➤ _before_destroy :ensure_not_referenced_by_any_lineitem
➤ private ➤ # ensure that there are no line items referencing this product ➤ def ensure_not_referenced_by_any_line_item ➤ unless line_items.empty? ➤ errors.add(:base, 'Line Items present') ➤ throw :abort ➤ end ➤ end end
## ActiveRecord::RecordNotFound
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
➤ rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
# GET /carts
# ...
private
# ...
➤ def invalid_cart
➤ logger.error "Attempt to access invalid cart #{params[:id]}"
➤ redirect_to store_index_url, notice: 'Invalid cart'
➤ end
Trả về giá trị lỗi khi không tìm thấy record bằng flash.
format.js { render :layout => false, :locals => { @current_item => @line_item } }
Cache list Cache có thể tăng hiệu năng sử dụng của hệ thống Cache giảm tải việc truy xuất trực tiếp vào database Cache tăng tốc độ truy cập ứng dụng web lên nhiều lần so với việc không sử dụng cache. Cache ứng dụng trong Rails web app là một ứng dụng web hoạt động theo hình thức Request và Responce dữ liệu đầu vào là URL và các tham số và kết quả trả về là nội dung HTML..
Session