OndrejK
OndrejK

Reputation: 337

ActiveRecord relationship model with Users and Organizations

I'm trying to find out what's the best logical way to model relationship between models. I have 4 models:

  1. User
  2. Product
  3. SlackTeam
  4. Organization

Here User has many Products, SlackTeams and Organizations, and SlackTeam belongs to User and has one Organization. Organization should belong to User and SlackTeam. Am I logically correct here?

The workflow is following:

Am I missing something?

Upvotes: 0

Views: 59

Answers (1)

oreoluwa
oreoluwa

Reputation: 5633

class User
  has_many :users_ogranizations
  has_many :organizations, through: :users_organizations
  has_many :products, through: :organizations
end

class Product
  belongs_to :organization
end

class Organization
  has_many :users_ogranizations
  has_many :users, through: :users_organizations
  has_many :products
end

class UsersOrganization
  belongs_to :user
  belongs_to :organization
end

# i'd rather use slack profile than team, because Organization and Team 
# already connotes the same thing.
class SlackProfile

end

You can handle your user's login however you like, I would prefer a kind of authentication service, though. All products that belongs to the organization is now accessible to the user, you can then filter the products with:

current_user.products.where(organization: Organization.last)

Upvotes: 0

Related Questions