sql - 加入多个表/约束或 has_one_through 与次要约束

标签 sql ruby postgresql activerecord ruby-on-rails-5.1

我为人为的例子道歉

一个有一个旅程,但与那个旅程没有直接联系,相反,他们通过他们的房子<联系起来office

A person belongs_to a house, a house has_many people

A person belongs_to a office, a office has_many people

A journey belongs_to a househouse has many journeys

A journey belongs_to a officeoffice has many journeys

                         +---------+
       +---Belongs-To---->         <----Belongs-To--+
       |                 |  House  |                |
       |  +-Has-Many-----+         +-----Has-Many-+ |
       |  |              +---------+              | |
       |  |                                       | |
+------+--v+                                     +v-+--------+
|          |                                     |           |
|  Person  |                                     |  Journey  |
|          |                                     |           |
+------+--^+                                     +^-+--------+
       |  |                                       | |
       |  |              +----------+             | |
       |  +-Has-Many-----+          +----Has-Many-+ |
       |                 |  Office  |               |
       +----Belongs-To--->          <---Belongs-To--+
                         +----------+

按照这个人为的例子,允许以下操作的最佳方法是什么:

person.journey 

或者除非使用多个表(使用 ruby​​ 哈希)的 Join 查询或使用具有额外表约束的 has_one_through

我们确实有一个 sql 查询,但如果可以的话,我们宁愿避免使用原始 sql,但它看起来像这样:

Person
.joins('INNER JOIN journeys
        ON journeys.office_id = person.office_id
        AND journeys.house_id = person.house_id')

最佳答案

很抱歉,我必须使用答案来获取更多信息。 有没有可能有这种情况?

| Person                      |   | Journey                     |
|----+-----------+------------|   |----+-----------+------------|
| id | office_id | house_id   |   | id | office_id | house_id   |
|----+-----------+------------|   |----+-----------+------------|
| 1  | 1         | 1          |   | 1  | 1         | 1          |
| 2  | 1         | 1          |   | 2  | 1         | 1          |
| 3  | 1         | 1          |   | 3  | 1         | 1          |
| 4  | 1         | 1          |   | 4  | 1         | 1          |

如何在这个案例的数据上找到特定人物的旅程?

如果您考虑 person = Person.find(1) 并使用 person.office_idperson.house_id 作为查找的键在 Journey 表中,您将获取 ID 1、2、3 和 4。不只是一次旅程。

根据您的回复,这种情况从未发生过,因为验证过滤器不允许。 因此,需要使用双外键访问 Journey 表:office_idhouse_id

最好的解决办法是

class Person < ActiveRecord::Base
  belongs_to :office
  belongs_to :house

  has_one :journey, foreign_keys: [:house_id, :office_id]

end

但是 rails 还不支持多外键。

一种可能的解决方法是为 Person 类定义实例方法 journey:

class Person < ActiveRecord::Base
  belongs_to :office
  belongs_to :house

  def journey
    Journey.where(office_id: office_id, house_id: house_id).last
  end

end

所以你可以调用person.journey

关于sql - 加入多个表/约束或 has_one_through 与次要约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51209288/

相关文章:

sql - 如何在列的 sql select 语句中插入新的 guid

java - 从 Java Web 应用程序调用 ruby​​ 库

sql - 在 postgres 中创建默认 UUID 生成器

xml - liquibase数据库重构xml createIndex

postgresql - Docker PostgreSQL 查询输出

sql - 如何使用批量插入仅根据文件扩展名导入文件?

sql - 我可以计算 SQL Server 中两个日期之间有多少个周末天数吗?

sql - Java 8 流与 jpa postgresql 排序依据。什么对性能更好?

ruby-on-rails - 如果需要过滤掉值,如何将 max_by 与可枚举值一起使用?

ruby-on-rails - 如何获取用Tire声明索引的类的列表?