【发布时间】:2015-01-28 09:02:13
【问题描述】:
我的产品索引返回所有产品,但我只想要那些有照片的产品。
控制器:
@products = Product.all.includes(:photos)
型号:
class Product < ActiveRecord::Base
has_many :photos
accepts_nested_attributes_for :photos, allow_destroy: true
end
class Photo < ActiveRecord::Base
has_attached_file :image
belongs_to :product
end
架构:
create_table "photos", force: true do |t|
t.integer "product_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.integer "user_id"
end
add_index "photos", ["product_id"], name: "index_photos_on_product_id", using: :btree
add_index "photos", ["user_id"], name: "index_photos_on_user_id", using: :btree
create_table "products", force: true do |t|
t.string "name"
t.integer "price"
t.integer "user_id"
t.boolean "sold", default: false
end
【问题讨论】: