2017-07-29 55 views
0

我有一個照片模型,它的照片可以在Bucket模型中。 Bucket模型是Facet模型的子類。Rails 5包含預先加載

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 

當我獲取桶中的照片,使用include我期望獲得Photo屬性+ Bucket屬性。但是我只拿到照片的屬性,我使用包括這樣的:

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