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

しかし、それは「MTI」ではなく「STI」用に予約されていることを理解しています。複雑なミックスインやプラグインを必要とせずにソリューションを機能させる方法はありますか? それとも不可能なだけですか?

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 に答える