2017-07-29 7 views
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 

答えて

1

いいえ、あなたは、ファセットを得ているが、あなたはassociation helpersを使用してそれらにアクセスする必要があります。しかし、私はこれだけのように含んで私が使用して、写真の属性を取得します

has_many :facetsは、個々のPhotoレコードにfacetsという関連付け方法を作成します。

考える:

photo = Photo.includes(:facets).find(7) 

あなたが行うことによって、関連するファセットを見ることができます:

photo.facets 
関連する問題