私はスカベンジャー ハント アプリに取り組んでいますが、それを正しくモデリングしているかどうかを知りたいだけです。ユーザーが参加できるハントのリストがあります。これらのハントは、カスタマイズ可能なテンプレートです (特定のタスクの追加/削除など)。
ユーザーが狩りに行くことを選択したとき、私はそれを「旅行」と呼んでいます。アソシエイトに関する Rails ガイドを読みましたが、トリップ モデルをユーザーとハント間の結合テーブルとして設定する必要があると考えています。これが私が考えていることです。
class Users
has_many :trips
has_many :hunts, :through => :trips
class Hunts
has_one :trip
has_many :users, :through => : trips
class Trip
belongs_to :users
belongs_to :hunts
次に、Trip テーブルをセットアップする移行は次のようになります。
def change
trip_name
user_id
hunt_id
end
2 つの質問があります。
- これは正しく見えますか?
- これを行うためのよりスマートな(またはよりエレガントな)方法はありますか?
更新:これが私がやったことです。
class Users
has_many :trips
has_many :hunts, :through => trips
class Hunts
has_one :trip
has_many :users, :through => trips
class Trip
belongs_to :hunts
belongs_to :users
その後
def change
create_table :trips do |t|
t.string :trip_name
t.references :user
t.references :hunt
end
add_index :trips, :hunt_id
add_index :trips, :user_id
add_index :trips, [:hunt_id, :user_id], :unique => true
end