mardi 4 août 2015

Rails controllers architecture with many to many relationships

I am developing a portal via RubyOnRails where pupils, teachers and parents can participate in different contests with their art works.

There are 3 entities: Contests, Categories (competitor categories / age groups) and Nominations (kinds of activity). Contest can have many Categories. Each ContestCategory can have many Nominations. Each Category can belong to several Contests. Every Nomination can belong to many ContestCategories. So I presume there is many-to-many relationship between Contests and Categories and many-to-many relationship between ContestCategories and Nominations. I've created following models: Contest (contest.rb), Category (category.rb), Nomination (nomination.rb), ContestCategory (contest_category.rb) and ContestCategoryNomination (contest_category_nomination.rb).

My models:

class Category < ActiveRecord::Base
  has_many :contest_categories
  has_many :contests, through: :contest_categories
end

class Contest < ActiveRecord::Base
  has_many :contest_categories
  has_many :categories, through: :contest_categories
  has_many :nominations, through: :contest_categories
  has_one :provision
end

class ContestCategory < ActiveRecord::Base
  belongs_to :contest
  belongs_to :category
  has_many :contest_category_nominations
  has_many :nominations, through: :contest_category_nominations
end

class ContestCategoryNomination < ActiveRecord::Base
  belongs_to :contest_category
  belongs_to :nomination
end

class Nomination < ActiveRecord::Base
  has_many :contest_category_nominations
  has_many :contest_categories, through: :contest_category_nominations
  has_many :contests, through: :contest_categories
end

I want to create an ajax-based modal window during creation of new Contest to link it with Category and select multiple Nominations that belong to this Category.

  1. What controllers should I create to satisfy has_many relationships between my models?
  2. What are the naming conventions (singular and plural) in rails to satisfy my relationships? For example, ContestsCategoriesController or ContestCategoryNominationsController or may be ContestCategoryNominationsController?
  3. What action method should I create in this controllers to invoke to render this modal window? Should it be new_category action in CategoriesController or new action in ContestsCategoriesController or new action in ContestsCategoriesNominationsController?


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire