我有一个has_one through
关联,返回nil。基本上,我有一个属于Review
模型的Enrollment
模型,该模型属于Section
模型。 Section
模型属于Cluster
。
我指的是Review has_one :section, through: :enrollment
- 这是有效的。但是,Review has_one :cluster, through :section
不起作用。请参阅下文,了解我如何定义模型:
Review
型号:
class Review < ActiveRecord::Base
belongs_to :reviewer,
class_name: 'Enrollment'
has_one :section,
through: :reviewer,
source: :section
has_one :cluster,
through: :section
end
Enrollment
型号:
class Enrollment < ActiveRecord::Base
belongs_to :section,
inverse_of: :enrollments
has_one :cluster,
through: :section
has_many :reviews,
foreign_key: :reviewer_id,
inverse_of: :reviewer
end
Section
型号:
class Section < Active::Record
belongs_to :cluster,
inverse_of: :sections
has_many :enrollments,
inverse_of: :section
end
最后,Cluster
模型:
class Cluster < ActiveRecord::Base
has_many :sections,
inverse_of: :cluster
end
在我的rails控制台中:
[30] pry(main)> r = Review.new(reviewer: Enrollment.all.sample)
Enrollment Load (3.5ms) SELECT "enrollments".* FROM "enrollments" => #<Review id: nil, submission_id: nil, created_at: nil, updated_at: nil, expires_at: nil, reviewer_id: 94, completed_at: nil, submitted: false>
[31] pry(main)> r.section
Section Load (0.5ms) SELECT "sections".* FROM "sections" INNER JOIN "enrollments" ON "sections"."id" = "enrollments"."section_id" WHERE "enrollments"."id" = $1 ORDER BY sections.class_number ASC LIMIT 1 [["id", 94]]
=> #<CampusSection id: 3, created_at: "2014-08-30 19:17:47", updated_at: "2014-08-30 19:17:47", cluster_id: 1, class_number: 157593622, type: "CampusSection", carmen_id: "218401898">
[32] pry(main)> r.section.cluster
Cluster Load (0.2ms) SELECT "clusters".* FROM "clusters" WHERE "clusters"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Cluster id: 1, created_at: "2014-08-30 19:17:47", updated_at: "2014-08-30 19:17:47", name: "Schuppe, Lueilwitz and Keeling 106", consent_agreement_id: 1>
[33] pry(main)> r.cluster
=> nil
感谢任何帮助。谢谢你们。
发布更新: 我的架构的相关部分:
ActiveRecord::Schema.define(version: 20140829191322) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "clusters", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
t.integer "consent_agreement_id"
end
create_table "enrollments", force: true do |t|
t.integer "enrollee_id", null: false
t.string "enrollee_type", null: false
t.integer "section_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "disenrolled_at"
t.boolean "disenrolled", default: false
t.boolean "consent", default: false
t.datetime "consented_at"
end
add_index "enrollments", ["disenrolled"], name: "index_enrollments_on_disenrolled", using: :btree
add_index "enrollments", ["disenrolled_at"], name: "index_enrollments_on_disenrolled_at", using: :btree
add_index "enrollments", ["enrollee_id", "enrollee_type", "section_id"], name: "index_enrollments_on_eid_and_etype_and_sid", unique: true, using: :btree
create_table "reviews", force: true do |t|
t.integer "submission_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "expires_at", null: false
t.integer "reviewer_id", null: false
t.datetime "completed_at"
t.boolean "submitted", default: false, null: false
end
add_index "reviews", ["reviewer_id"], name: "index_reviews_on_reviewer_id", using: :btree
add_index "reviews", ["submission_id", "reviewer_id"], name: "index_reviews_on_submission_id_and_reviewer_id", unique: true, using: :btree
add_index "reviews", ["submission_id"], name: "index_reviews_on_submission_id", using: :btree
create_table "sections", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "cluster_id", null: false
t.integer "class_number", null: false
t.string "type", null: false
t.string "carmen_id"
end
add_index "sections", ["carmen_id"], name: "index_sections_on_carmen_id", using: :btree
add_index "sections", ["class_number"], name: "index_sections_on_class_number", using: :btree
end
答案 0 :(得分:1)
我想在你可以直接获得相应的集群之前需要保留review
,不知道为什么......