我有3个班,Products
,ProductDetails
,ProductStatuses
显示孩子的家长上显示计数 - 轨道4
第一个具有一般的数据。
第二个库存/库存和产品的详细信息。
第三个决定是否可以销售ProductDetail
(产品系列)。
我想在Products
索引(常规视图)中显示产品的库存计数。
该机型的优势如下:
class Product < ActiveRecord::Base
has_many :product_details
end
class ProductDetail < ActiveRecord::Base
belongs_to :product, foreign_key: :product_id
belongs_to :product_status, foreign_key: :status
end
class ProductStatus < ActiveRecord::Base
has_many :product_details
end
的模式是:
create_table "product_details", force: :cascade do |t|
t.integer "product_id", limit: 4
t.string "serial", limit: 255
t.string "comment", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", limit: 4
end
create_table "product_statuses", force: :cascade do |t|
t.boolean "available"
t.string "name", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "products", force: :cascade do |t|
t.string "name", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "published"
end
我用Rails初学者。
我不知道如何让孩子数。这听起来像是一个常见问题,但我无法找到答案。
需要帮助:(
感谢
我知道SQL确实不错,但这种下降我下面的错误:协会命名为“product_statuses”在产品详情没有被发现;也许你拼错了吗? –
明白了,我解决了它,但查询只给出了具有可用库存的产品,我希望所有产品都具有计数,包括0:'( –