我有以下型号:
用户:
User
has_and_belongs_to_many :user_jobs, foreign_key: "user_id", class_name: "Job"
has_many :jobs, through: :locations
has_many :customers, through: :locations
has_and_belongs_to_many :locations
位置:
Location
has_and_belongs_to_many :users
has_and_belongs_to_many :customers
has_many :jobs
客户:
Customer
has_many :jobs, dependent: :destroy
has_and_belongs_to_many :locations
作业:
Job
belongs_to :customer
belongs_to :location
has_and_belongs_to_many :users
这是我的Schema.rb文件
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more
migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180308214356) do
create_table "accounts", force: :cascade do |t|
t.string "name"
t.text "company_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "stripe_publishable_key"
t.text "stripe_account_id"
t.text "twilio_account_sid"
t.text "twilio_auth_token"
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "location_id"
t.index ["location_id"], name: "index_categories_on_location_id"
end
create_table "customers", force: :cascade do |t|
t.string "first_name"
t.string "middle_initial"
t.string "last_name"
t.datetime "updated_at", null: false
t.integer "account_id"
t.text "stripe_customer_id"
t.index ["account_id"], name: "index_customers_on_account_id"
end
create_table "customers_locations", id: false, force: :cascade do |t|
t.integer "location_id", null: false
t.integer "customer_id", null: false
end
create_table "drafts", force: :cascade do |t|
t.string "phone_one"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "jobs", force: :cascade do |t|
t.date "date"
t.integer "time"
t.boolean "time_sensitive"
t.text "address_line_one"
t.text "address_line_two"
t.string "city"
t.string "state"
t.string "zip"
t.text "special_instructions"
t.text "description"
t.text "work_completed"
t.text "billing_information"
t.text "notes"
t.string "status"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["customer_id"], name: "index_jobs_on_customer_id"
t.index ["location_id"], name: "index_jobs_on_location_id"
end
create_table "jobs_users", id: false, force: :cascade do |t|
t.integer "user_id", null: false
t.integer "job_id", null: false
end
create_table "locations", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "account_id"
t.index ["account_id"], name: "index_locations_on_account_id"
end
create_table "locations_users", id: false, force: :cascade do |t|
t.integer "user_id", null: false
t.integer "location_id", null: false
end
create_table "users", 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.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "location_id"
t.integer "account_id"
t.integer "role_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["location_id"], name: "index_users_on_location_id"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["role_id"], name: "index_users_on_role_id"
end
end
通过这种模型结构,我可以用两种不同的方式查询作业模型。第一个user.user_jobs和user.jobs
。这两个查询都返回两个不同的结果集。我需要能够与客户做同样的事情。我当前可以查询user.customers
,但我不知道我应该使用什么语法来使user.user_customers工作,以便查询与用户加入'相关工作,然后抓住与这些工作相关的客户。我当前的查询加入了用户'位置和抓取与这些位置相关联的客户。提前谢谢!
答案 0 :(得分:0)
has_many :user_job_customers, through: :user_jobs, source: 'customers'
has_many :location_customers, through: :locations, source: 'customers'
我想你需要:源选项