ruby-on-rails - Rails:自引用关联让我头晕目眩
问题描述
我目前正在开展一个使用 Ruby on Rails 的小型学校项目,但在让我的自引用关联正常工作时遇到了一些麻烦。
语境
我的网络应用程序的预期功能是让用户发布房屋/公寓供其他用户搜索和出租。由于我遇到了特定关联的问题,因此我正在使用一个完全精简的版本,它只有两个模型,用户和租赁。
我想要完成的事情
理想情况下,当一个人第一次在网站上注册时,会创建一个 User 对象来保存他们的信息,例如电子邮件和密码。然后,用户可以发布列表或搜索列表。
一旦创建了帖子并且另一个用户决定租用发布的房屋,就会创建一个 Lease 对象,该对象包含发布用户的 ID 以及租赁用户的 ID,别名分别为“landlord_id”和“tenant_id” .
现在应该根据是否有任何 ID 为 Landlord 或 Tenant 的 Lease 对象来将 User 标识为 User、Landlord 或 Tenant(或 Landlord 和 Tenant)。此标识将用于确定用户是否可以访问网站的其他区域。
userFoo.leases
这应该给我一个与用户 ID 相关联的所有 Lease 对象的列表,无论它是作为房东还是租户。
userFoo.tenants
这应该给我一个任何用户对象的列表,其 ID 与作为租户的 userFoo 的 ID 通过租赁相关联,如果我要求房东,则相反。
编码
用户类
class User < ApplicationRecord
has_many :tenants, class_name: "Lease", foreign_key: "landlord_id"
has_many :landlords, class_name: "Lease", foreign_key: "tenant_id"
end
租赁类
class Lease < ApplicationRecord
belongs_to :landlord, class_name: "User"
belongs_to :tenant, class_name: "User"
end
用户表迁移
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password_digest
t.timestamps
end
end
end
租赁表迁移
class CreateLeases < ActiveRecord::Migration[6.0]
def change
create_table :leases do |t|
t.references :landlord, null: false, foreign_key: {to_table: :users}
t.references :tenant, null: false, foreign_key: {to_table: :users}
t.timestamps
end
end
end
数据库模式
ActiveRecord::Schema.define(version: 2020_10_18_005954) do
create_table "leases", force: :cascade do |t|
t.integer "landlord_id", null: false
t.integer "tenant_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["landlord_id"], name: "index_leases_on_landlord_id"
t.index ["tenant_id"], name: "index_leases_on_tenant_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "leases", "users", column: "landlord_id"
add_foreign_key "leases", "users", column: "tenant_id"
end
怎么了?
userFoo.leases
通常,用户将通过将其 ID 与租约关联为“user_id”来拥有_many 租约。但是,由于我使用的是“tenant_id”和“landlord_id”,因此该命令失败,因为它在 Leases 表中找不到“user_id”。
userFoo.tenants
此命令为我提供了一个列表,其中列出了 userFoo 的 ID 关联为“landlord_id”的所有 Lease 对象,而不是与 userFoo 的 ID 关联的所有用户对象作为租户。要按原样检索租户,我必须使用以下命令:
userFoo.tenants.first.tenant
结论 我有点难以理解这些更深、更复杂的关联,我花了一些时间试图找到关于 has_many 的详细参考,涵盖了所有论点,但我真正能找到的只是一些小的博客文章参考 guides.rubyonrails.com 上的“员工”和“经理”示例。我认为一个问题是我不确定我是否在我的表模式中正确反映了我的模型关联。
如果有人能指出我正确的方向,我会非常乐意自学。我也对替代解决方案持开放态度,但前提是我无法从此设置中获得我想要的功能,因为我的导师特别要求我以这种方式尝试
提前感谢您的帮助!非常感谢。
解决方案
根据您的要求,您可以这样尝试:
# app/models/user.rb
class User < ApplicationRecord
has_many :owned_properties, class_name: "Property", foreign_key: "landlord_id"
has_many :rented_properties, class_name: "Property", foreign_key: "tenant_id"
end
在这里,我声明了两个具有相同表但不同外键的关联。
# app/models/property.rb
class Property < ApplicationRecord
belongs_to :landlord, class_name: "User"
belongs_to :tenant, class_name: "User"
end
在这里,我使用了一张桌子,该用户可以发布一处房产,其中房东是房屋的所有者,然后您可以将正在收租的租户添加到一处房产。
# db/migrations/20201018054951_create_users.rb
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name, null: false
t.string :email, null: false, index: true
t.string :password_digest, null: false
t.timestamps
end
end
end
以上是您的用户表迁移。
# db/migrations/20201018055351_create_properties.rb
class CreateProperties < ActiveRecord::Migration[6.0]
def change
create_table :properties do |t|
t.references :landlord, foreign_key: {to_table: :users}, null: false
t.references :tenant, foreign_key: {to_table: :users}
t.timestamps
end
end
end
以上是您的属性表迁移。
# db/schema.rb
ActiveRecord::Schema.define(version: 2020_10_18_055351) do
create_table "properties", force: :cascade do |t|
t.bigint "landlord_id", null: false
t.bigint "tenant_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["landlord_id"], name: "index_properties_on_landlord_id"
t.index ["tenant_id"], name: "index_properties_on_tenant_id"
end
create_table "users", force: :cascade do |t|
t.string "name", null: false
t.string "email", null: false
t.string "password_digest", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email"
end
add_foreign_key "properties", "users", column: "landlord_id"
add_foreign_key "properties", "users", column: "tenant_id"
end
如果要获取用户的所有拥有属性,请使用user.owned_properties
.
如果要获取用户的所有租用属性,请使用user.rented_properties
.
^^ 在这两种情况下,您都会获得Property
类对象。
如果您想获得房产的房东,请使用property.landlord
.
如果您想获得物业的租户,请使用property.tenant
.
^^ 在这两种情况下,您都会获得User
类对象。
如果您愿意,您可以将其他属性,如:name
、、price
等添加到属性表中。
我想,这会对你有所帮助。谢谢 :) 快乐编码 :)
推荐阅读
- vue.js - 如何在 vue-class-component 中访问 v-model
- arrays - 神经网络输出形状不匹配
- azure-active-directory - 自定义 Azure B2C 注册策略 - 业务逻辑 API 的客户端凭据授权流程
- c++ - 如何在 C++ 中重新声明已创建的对象值
- sql - 更新列并立即使用结果更新同一语句中的第二列
- php - 如何从 XML id 中获取价值 - PHP
- sql - SELECT 附近的输出参数语法不正确
- css - 淡入后按钮闪烁
- flutter - Flutter 中使用 BLoC 的异步请求
- excel - Excel 2013 VBA为A列中包含数据的每一行选择单元格P