私は2つの基本的なモデルを持っています:スタジアムとオーナー
これら2つのモデル間の関係は次のとおりです。
スタジアム:_
belongs_to :owner
所有者:
has_many :stadiums
ここで重要なのは、所有者にはカテゴリも関連付けられており、ここでowner_categoryモデルが登場します。
所有者:
has_and_belongs_to_many :owner_categories,
:join_table => 'l_owners_owner_categories',
そしてOwnerCategory:
has_and_belongs_to_many :owners, :join_table => 'l_owners_owner_categories'
基本的に、
OwnerCategoryテーブルは次のようになります。
id,name
1,sport
2,kids
だから、私の質問は:
ユーザーに都市とカテゴリを選択させるとすると、所有者が指定したカテゴリを持つその都市からすべてのスタジアムを取得するにはどうすればよいですか?
したがって、たとえば:
次のスタジアムがある場合:
id,name,city,owner_id
1,'soccer stadium','New York',5
2,'music stadium','New York',4
2,'music stadium','San Francisco',4
次の所有者:
id,name
4, 'John'
5, 'Peter'
次のOwnersCategoriesテーブル:
id,name,description
1,'sports','this category is associated with stadiums that support sports'
2,'music','this category is associated with stadiums that support music'
そして、次の結合テーブル:
owner_id,owner_category_id
5, 1
ユーザーが「ニューヨーク」と「スポーツ」を選択すると、次のスタジアムが表示されます。
1,'soccer stadium','New York',5