We are already capturing businesses owned by politicians, we want to also capture those they're affiliated to, either those they co own or those their close family members own
Proposal(feel free to do it any other way)
Create a new model called Affiliation that represents the relationship between a politician and a business. This model will act as a join table between the Politician and Business models.
class Affiliation < ApplicationRecord
belongs_to :politician
belongs_to :business
enum relationship: { owned: 0, family_owned: 1, co_owned: 2 }
end
The relationshipenum represents the different types of affiliations a politician can have with a business. The possible values can be:
owned: The politician directly owns the business.
family_owned: A close family member of the politician owns the business.
co_owned: The politician co-owns the business with other people.
Update the Politician model to establish the many-to-many relationship with the Business model through the Affiliation model.
class Politician < ApplicationRecord
has_many :affiliations
has_many :businesses, through: :affiliations
end
Update the forms of creating a business to capture the said politician, then to capture the business details and affiliation relationship enum
Update the Business model to establish the many-to-many relationship with the Politician model through the Affiliation model.
class Business < ApplicationRecord
has_many :affiliations
has_many :politicians, through: :affiliations
end
On each politician's page, show all businesses according to their affiliations, eg (rough example, so edit according to what we currently have, maintaining our current style, or making it better)
Politician Show View (app/views/politicians/show.html.erb):
<h1><%= @politician.name %></h1>
<h2>Owned Businesses</h2>
<ul>
<% @politician.affiliations.where(relationship: :owned).each do |affiliation| %>
<li><%= link_to affiliation.business.name, business_path(affiliation.business) %></li>
<% end %>
</ul>
<h2>Family Owned Businesses</h2>
<ul>
<% @politician.affiliations.where(relationship: :family_owned).each do |affiliation| %>
<li><%= link_to affiliation.business.name, business_path(affiliation.business) %></li>
<% end %>
</ul>
<h2>Co-owned Businesses</h2>
<ul>
<% @politician.affiliations.where(relationship: :co_owned).each do |affiliation| %>
<li><%= link_to affiliation.business.name, business_path(affiliation.business) %></li>
<% end %>
</ul>
In each business page as well, show the affiliations of that business
Business Show View (app/views/businesses/show.html.erb - again just a rough example, make the styling better):
Write a rake task to update all existing businesses to create a new affiliation for each, with the relationship being :owned since all current businesses are owned by said politicians
We are already capturing businesses owned by politicians, we want to also capture those they're affiliated to, either those they co own or those their close family members own
Proposal(feel free to do it any other way)
Affiliation
that represents the relationship between a politician and a business. This model will act as a join table between thePolitician
andBusiness
models.The
relationship
enum
represents the different types of affiliations a politician can have with a business. The possible values can be:owned
: The politician directly owns the business.family_owned
: A close family member of the politician owns the business.co_owned
: The politician co-owns the business with other people.Politician
model to establish the many-to-many relationship with theBusiness
model through theAffiliation
model.The last part can look something like this
Business
model to establish the many-to-many relationship with thePolitician
model through theAffiliation
model.Politician Show View (
app/views/politicians/show.html.erb
):Business Show View (
app/views/businesses/show.html.erb
- again just a rough example, make the styling better):relationship
being:owned
since all current businesses are owned by said politicians