현재 Order, OrderItems 및 Products 모델이 있습니다. OrderItems에 소계라고하는 메소드를 정의하려고합니다.이 메소드는 수량 값에 가격 (관계 product.price를 통해)을 리턴합니다.레일에서 모델 메소드 정의하기
어떻게 수행 할 수 있습니까? 관계를 통해 열과 행에 액세스하는 방법을 모른다. 당신의 도움에 대한
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
validates :order_id, presence: true
validates :product_id, presence: true
def subtotal
quantity * product.price
end
end
테이블 스키마
create_table "order_items", force: :cascade do |t|
t.integer "product_id"
t.integer "order_id"
t.integer "quantity"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
감사합니다. OrderItems에와 제품 사이의 관계에 대한 약간의 확신
는'OrderItems' 테이블의 적어도 스키마 여기 게시물을 고려해 다음과 같이
당신은 그것을 호출 할 수 있습니다. – developer033
고맙습니다, 끝났습니다 ... –
나는 테이블의 열을 의미합니다 ... – developer033