Rails 4 where嵌套连接表的子句

时间:2017-06-20 17:56:38

标签: ruby-on-rails database postgresql ruby-on-rails-4

目前我有这个问题:

Mother.joins(camp: [{ sub_center: [{ health_center: :block }] }])

它基本上通过motherblockcamp表将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

1 个答案:

答案 0 :(得分:2)

您始终可以使用字符串方法来引用列,而不是符号1。

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 } )