'Setup and communicate between multiple rails user profile/roles
A marketplace allows users to buy and sell items. It construct of User model and Item model. Each has many attributes, and their association is:
user.rb
has_many :items, dependent: :destroy, inverse_of: :user
item.rb
belongs_to :user, optional: true, counter_cache: true, inverse_of: :items
Each user show view is alike a profile where it shows all selling items.
Now, I want to introduce 2 type of users. Single seller/buyer (sell and buy few items) and Business (sell bulk/buy small or bulk items).
I created 2 models.
Client
belongs_to :user
has_many :items, dependent: :destroy, inverse_of: :client
Business
belongs_to :user
has_many :items, dependent: :destroy, inverse_of: :business
NOTE: While Business and Client share few attributes, they have many different ones. That's why I created 2 models.
And User
has_one :client, dependent: :destroy
has_one :business, dependent: :destroy
Also Item
belongs_to :client, optional: true, counter_cache: true, inverse_of: :items
belongs_to :business, optional: true, counter_cache: true, inverse_of: :items
While I "feel" it is a good thing to have separate models, I "fell" it's complicated when it comes to item creations and each entity show view.
Required flow
When a user signin, it is redirect to /roles/new view and choose to create a Client, a Business or both roles/profiles.
After user have one or both roles, is able to create an item.
Before in item_controller.rb I would do
def new
current_user.business.items.new
end
Now, I'm not sure. I want to differentiate between which role create an item!
def new
if current_user.client
@item = current_user.client.items.new
elsif current_user.business
@item = current_user.business.items.new
else
current_user.items.new
end
end
Does it make sense???!!!
Is the user a Client, Business or both. This is needed to show client's items in its' show view, same goes to Business.
While most likely User with either Business or Client role would not create the other role but it could happen. A user have both Business and Client roles.
A user with one or more roles should create an item, this item belongs to the role/profile that created.
I think of it like, an application has Developer and Employer profiles. Both belongs to User model and both could create/has many Projects.
Am I making the process complicated for myself or it is solvable?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
