Skip to content

Instantly share code, notes, and snippets.

@danecjensen
Created April 21, 2023 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danecjensen/fd0e8d9964022c8ad2e6d63f8e4bd93a to your computer and use it in GitHub Desktop.
Save danecjensen/fd0e8d9964022c8ad2e6d63f8e4bd93a to your computer and use it in GitHub Desktop.
Build the models for marketplace

Based on your description, here's a list of ActiveRecord models you'll need for your Airbnb-like marketplace application:

  1. User
  2. Role
  3. Service
  4. Booking
  5. Review

Now, let's define the models along with their associations:

# app/models/user.rb
class User < ApplicationRecord
  has_many :services, foreign_key: :supplier_id
  has_many :bookings, foreign_key: :customer_id
  has_many :reviews, through: :bookings
  belongs_to :role
end

# app/models/role.rb
class Role < ApplicationRecord
  has_many :users
end

# app/models/service.rb
class Service < ApplicationRecord
  belongs_to :supplier, class_name: 'User'
  has_many :bookings
  has_many :reviews, through: :bookings
end

# app/models/booking.rb
class Booking < ApplicationRecord
  belongs_to :customer, class_name: 'User'
  belongs_to :service
  has_one :review
end

# app/models/review.rb
class Review < ApplicationRecord
  belongs_to :booking
  has_one :customer, through: :booking
  has_one :service, through: :booking
end

This setup will provide the necessary models and associations for your application. Users can be suppliers or customers, services are provided by suppliers, customers can make bookings for services, and reviews can be written by customers after a booking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment