如何从收据实例中获取数量?
我有 3 个表:receipt,receipt_foods,
我想从收据实例中获取receipt_foods 列的食物,例如数量。
class Receipt
# columns: id, place
has_many :receipt_foods
has_many :foods, through: :receipt_food
end
class ReceiptFood
# columns: id, quantity, receipt_id, food_id
belongs_to :receipt
belongs_to :food
end
class Food
# columns: id, name
has_many :receipt_foods
has_many :receipts, through: :receipt_food
end
我想这样回应json:
{ "receipts": [
"receipt": {
"id": 1,
"place": "supermarket",
"foods": [ {
"food": {
"id": 1,
"quantity": 12, // how to echo this ?
"name": "apple" }
....
}