活动记录has_many

时间:2015-07-07 11:04:38

标签: mysql ruby-on-rails activerecord

我需要列出所有用户使用他们的电子邮件和产品总数

销售的总产品
java -jar jsonschema2pojo-cli-0.4.13.jar --help

订单     id user_id     1 1     2 2

class User < ActiveRecord::Base
  # id, location_id, email
  has_many :orders, dependent: :destroy
end

class Order < ActiveRecord::Base
#status, payment_mode, total_cost, user_id
  has_many  :order_items, dependent: :destroy
  belongs_to :user
end

class OrderItem < ActiveRecord::Base
# order_id, product_id, quantity,total_price
  belongs_to :order
  belongs_to :product
end

User
id email
1  test@gmail.com
2  test2@gmail.com

我需要显示像     name product_count     test@gmail.com 10     test1@gmail.com 5

1 个答案:

答案 0 :(得分:1)

请试试这个:

{{1}}