首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统计协会

统计协会
EN

Stack Overflow用户
提问于 2021-02-17 10:02:08
回答 1查看 32关注 0票数 0

我是RoR新手,我需要一些关联方面的帮助。我使用的是rails 6.0.3.4和ruby2.7.0。

用户可以创建案例,案例属于某一地区。地区属于一个州。必须是这样,因为案件不属于一个国家。

现在我想给出每个州的特定诊断的病例数。我得用地区来为一个州弄到所有的案子。我该如何建造这个地方(.)情况如何?

代码语言:javascript
复制
<!-- State -->
    <div class="card">
      <div class="card-body">
        <h5 class="card-title">State</h5>
        <p><%= State.find(1).titel%> (<%= @diagnosis.cases.where(...).count %>)</p>
      </div>
    </div>

我的模特

case.rb

代码语言:javascript
复制
class Case < ApplicationRecord
  before_create :set_pseud
  belongs_to :user
  belongs_to :diagnosis
  belongs_to :district
  belongs_to :report, optional: true
end

district.rb

代码语言:javascript
复制
class District < ApplicationRecord
  has_many :users
  has_many :cases
  has_many :reports
  belongs_to :state
end

state.rb

代码语言:javascript
复制
class State < ApplicationRecord
  has_many :districts
  has_many :users
end

以更好地理解我的schema.rb

代码语言:javascript
复制
ActiveRecord::Schema.define(version: 2021_02_11_140244) do

  create_table "cases", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.string "gender"
    t.date "birthdate"
    t.string "place_of_residence"
    t.string "diagnosis"
    t.bigint "user_id"
    t.datetime "confirmed_at"
    t.datetime "created_at", precision: 6
    t.datetime "updated_at", precision: 6, null: false
    t.bigint "diagnosis_id"
    t.bigint "district_id"
    t.bigint "report_id"
    t.string "pseud"
    t.index ["diagnosis_id"], name: "index_cases_on_diagnosis_id"
    t.index ["district_id"], name: "index_cases_on_district_id"
    t.index ["pseud"], name: "index_cases_on_pseud"
    t.index ["report_id"], name: "index_cases_on_report_id"
    t.index ["user_id"], name: "index_cases_on_user_id"
  end

  create_table "diagnoses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.string "illness"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "districts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.string "name"
    t.string "place"
    t.integer "postal_code"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.bigint "state_id", null: false
    t.index ["state_id"], name: "index_districts_on_state_id"
  end

  create_table "reports", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.bigint "district_id"
    t.text "comment"
    t.datetime "date"
    t.bigint "user_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["district_id"], name: "index_reports_on_district_id"
    t.index ["user_id"], name: "index_reports_on_user_id"
  end

  create_table "states", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.string "titel"
    t.string "abbr"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "role"
    t.string "first_name"
    t.string "last_name"
    t.bigint "district_id"
    t.bigint "state_id"
    t.index ["district_id"], name: "index_users_on_district_id"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["state_id"], name: "index_users_on_state_id"
  end

end
EN

回答 1

Stack Overflow用户

发布于 2021-02-17 10:48:16

您应该在状态中添加一个进一步的关联:

代码语言:javascript
复制
has_many :cases, through: :districts

与其在视图中查找状态,不如在控制器中这样做,并将其传递给实例变量中的视图:

代码语言:javascript
复制
@state = State.find(params[:id])

我假设您在这里使用的是显示操作,而不是出于某种原因手动编写状态ID。

然后你可以这样做:

代码语言:javascript
复制
@state.cases.where(diagnoses: { id: @diagnosis.id }).count

或者,如果您愿意的话,可以跳过@diagnosis上的.id

代码语言:javascript
复制
@state.cases.where(diagnoses: { id: @diagnosis }).count
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66239662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档