Reputation: 41
Just looking to know what the best practice is to do the following.
I have two models
Group
id int pk
name string
Company
id int pk
name string
A group instance can have zero or many instances of companies
A company instance can have zero or many instances of groups
Validate that a company cannot belong to the same group more than once
use a has_and_belongs_to_many association (No intervening model) ?
use a has_many :through (setup an organisation model) ?
I want to setup restful views to:
Group
/groups (Show All Group Names)
/groups/1 (Show one group)
/groups/new (Add Group)
etc...
Companies
/companies/1 (show all companies)
/companies/1 (Show company)
/companies/new (Add Company)
etc...
The issue i am trying to get around is how do I setup the following views with the models ?
(Show all companies for a specified group) /groups/1/companies ??
(Add, update and remove companies for a specified group ?!)
Upvotes: 1
Views: 117
Reputation: 8503
You want to use nested routes
e.g. write in your routes.rb
file
resources :groups do
resources :companies
end
then in your model a company belongs_to a group and a group has_many companies.
make sure to check out the guides for more information on routing.
Upvotes: 2