В настоящее время у меня есть модель Order, OrderItems и Products. Я хочу определить метод под названием subtotal в OrderItems, который будет возвращать значение количества, умноженное на цену (через отношение product.price).Определение методов модели в Rails
Как я мог это сделать? Я не знаю, как обращаться к столбцам и столбцам через отношения.
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' таблицы. – developer033
Спасибо, сделано .. –
Я имею в виду столбцы вашего стола ... – developer033