如何从收据实例中获取数量?
我有3张桌子:收据,收据_食物,食物
我想从收据实例中获取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" }
....
}
答案 0 :(得分:0)
你可以获得如下的数量
receipt_foods = @receipt.receipt_foods
receipt_foods.each do |receipt_food|
puts receipt_food.quantity
end