2

我目前正在使用 Rails 3.2 开发一个应用程序并且遇到了一些问题。我知道这已经被问过数百次了,但我找不到解决它的答案。这是一个类似的 ER:http: //i.stack.imgur.com/x5V0G.png

很明显我正在尝试做的事情。我希望该协会的内容如下:

Supplier.first.theatres.first.events.first.orders
TourEvent.first.orders
Tour.first.orders

现在能够像这样定义我的模型会很好:

class Event < ActiveRecord::Base
  has_many :orders
  belongs_to :eventable, polymorphic: true

  # id, eventable_id, eventable_type, title, date, price
end

class TourEvent < Event
  belongs_to :tour

  # id, tour_id, rendezvous, guide_name
end

class Tour < ActiveRecord::Base
   has_many :events, class_name: 'TourEvent'

  # id, name, venue, duration
end

但我知道这是为“STI”而不是“MTI”保留的。任何想法如何让我的解决方案在不需要复杂的 mixins 或插件的情况下工作?还是只是不可能?

4

1 回答 1

0

我认为你可以做这样的事情:

suppliers 
has_many :events

events
belongs_to :suppliers
belongs_to :institutions
has_many :orders
# id, supplier_id, institution_id, ...

institutions
# id, type, title, ...
types: theatre, club, tour

orders
belongs_to :events
# id, event_id

然后,您可以访问事件订单:

Supplier.first.events.first.orders
于 2012-02-29T12:13:08.157 回答