通过连接表连接的Rails模型

时间:2022-09-22 18:09:15

Is this possible?

这可能吗?

In an events system an event can have multiple times. (ie, if it is a 3 day event and each day it's at a different time). Each time has a place associated with it. Finally, each place has an address associated with it. Now, can I reference those addresses through my event model?

在事件系统中,事件可以有多次。 (即,如果是3天的活动,每天都是在不同的时间)。每次都有一个与之相关的地方。最后,每个地方都有一个与之相关的地址。现在,我可以通过我的活动模型引用这些地址吗?

I'm thinking something conceptually like this:

我在想这样的概念:

class Event < ActiveRecord::Base
    has_many :TimePlaces
    has_many :Places :through => :TimePlaces
    has_many :Addresses :through => :PlaceAddresses :through => :Places

1 个解决方案

#1


This is the right syntax.

这是正确的语法。

class Event < ActiveRecord::Base
    has_many :time_places
    has_many :places, :through => :time_places
    has_many :addresses, :through => :places

Despite this should work, you might want to redesign your database. Running a query with too many joins requires an intensive database elaboration and drastically slows down your application.

尽管这应该可行,但您可能需要重新设计数据库。运行具有太多联接的查询需要密集的数据库详细说明并且会大大降低应用程序的速度。

#1


This is the right syntax.

这是正确的语法。

class Event < ActiveRecord::Base
    has_many :time_places
    has_many :places, :through => :time_places
    has_many :addresses, :through => :places

Despite this should work, you might want to redesign your database. Running a query with too many joins requires an intensive database elaboration and drastically slows down your application.

尽管这应该可行,但您可能需要重新设计数据库。运行具有太多联接的查询需要密集的数据库详细说明并且会大大降低应用程序的速度。