当前我有此查询:
Mother.joins(camp: [{ sub_center: [{ health_center: :block }] }])
它通过
mother
、block
和camp
表将sub_center
表连接到health_center
表(我知道这需要很多东西)。我想做这样的事情:Mother.joins(camp: [{ sub_center: [{ health_center: :block }] }]).where(sub_center_id: 1)
但是,我得到
sub_center_id
是一个未定义的列PG::UndefinedColumn: ERROR: column mothers.sub_center_id does not exist
如何让它查询
camp.sub_center_id
而不是mother.sub_center_id
?换句话说,如何在嵌套联接表上执行where子句?下面是给定表的架构:
create_table "mothers", force: :cascade do |t|
t.integer "camp_id"
t.integer "eligible_couple_id"
t.string "pcts_id"
t.integer "thumb_id"
t.string "survey_id"
t.string "necklace_id"
t.integer "necklace_replacement_number", default: 0
t.boolean "facility_referral_true", default: false
t.float "weight_at_las_pnc"
t.integer "created_by"
t.integer "updated_by"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "reg_id"
t.string "nfc_data"
t.datetime "date_of_registration"
t.integer "status", default: 1, null: false
t.datetime "deleted_at"
t.datetime "completed_at"
end
create_table "camps", force: :cascade do |t|
t.string "english_name"
t.string "local_name"
t.float "latitude"
t.float "longitude"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "created_by"
t.integer "updated_by"
t.integer "sub_center_id"
t.integer "week_of_the_month"
t.integer "status", default: 1
t.integer "day"
t.datetime "deleted_at"
end
create_table "health_centers", force: :cascade do |t|
t.integer "block_id"
t.string "english_name"
t.string "local_name"
t.boolean "phc_true", default: false
t.boolean "ipd_true", default: false
t.boolean "fru_true", default: false
t.boolean "always_open_true", default: false
t.string "phone_number"
t.integer "created_by"
t.integer "updated_by"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 1
t.datetime "deleted_at"
end
create_table "sub_centers", force: :cascade do |t|
t.integer "health_center_id"
t.string "english_name"
t.string "local_name"
t.boolean "phc_true", default: false
t.boolean "ipd_true", default: false
t.boolean "fru_true", default: false
t.boolean "always_open_true", default: false
t.string "phone_number"
t.float "latitude"
t.float "longitude"
t.integer "created_by"
t.integer "updated_by"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 1
t.datetime "deleted_at"
end
create_table "blocks", force: :cascade do |t|
t.string "english_name"
t.string "local_name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "created_by"
t.integer "updated_by"
t.integer "status", default: 1
t.datetime "deleted_at"
end
最佳答案
您始终可以使用string方法来引用列,而不是符号列。
Mother.
joins(camp: { sub_center: { health_center: :block } }).
where('camps.sub_center_id = ?', 1)
或者用符号法
Mother.
joins(camp: { sub_center: { health_center: :block } }).
where(camps: { sub_center_id: 1 } )
关于ruby-on-rails - 嵌套联接表上的Rails 4 where子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44659998/