0
写真はバケツモデルでも写真にもできるフォトモデルがあります。バケットモデルは、ファセットモデルのサブクラスです。Rails 5にはeagerloadingが含まれています
class Photo < ActiveRecord::Base
has_many :facets
has_one :bucket
end
class Facet < ApplicationRecord
# validates_uniqueness_of :user, :scope => :photo_id
belongs_to :photo
belongs_to :user
end
class Bucket < Facet
belongs_to :photo
belongs_to :user
validates :user_id, uniqueness: true
end
バケットにある写真をフェッチする際に、インクルードを使用すると、写真の属性とバケットの属性を取得することが予想されます。
Photo.includes(:facets).find(7)
ファセットテーブル:
create_table "facets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci" do |t|
t.integer "user_id"
t.integer "photo_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "type"
t.string "comment"
t.integer "tag_id"
t.bigint "photos_id"
t.index ["photos_id"], name: "index_facets_on_photos_id"
t.index ["user_id", "type", "comment", "tag_id"], name: "index_facets_on_user_id_and_type_and_comment_and_tag_id", unique: true
end