0

如何从收据实例中获取数量?

我有 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" } 
....
  }
4

1 回答 1

0

你可以得到如下的数量

receipt_foods = @receipt.receipt_foods 
receipt_foods.each do |receipt_food|
  puts receipt_food.quantity 
end
于 2013-01-27T06:09:49.387 回答