Skip to content

Commit

Permalink
Install and create the active storage migrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-noori committed Oct 9, 2023
1 parent fb7a019 commit e207b44
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.1.4'
ruby '3.2.2'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem 'rails', '~> 7.0.8'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::ReservationsController < ApplicationController
# GET /reservations
def index
user_id = params[:user_id]
@reservations = Reservation.includes(:course).where(user_id: user_id)
@reservations = Reservation.includes(:course).where(user_id:)

render json: @reservations, include: :course
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Course < ApplicationRecord
has_many :reservations
has_many :users, through: :reservations

has_one_attached :image

validates :name, presence: true
validates :description, presence: true
validates :image, presence: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
32 changes: 31 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 15 additions & 23 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
# Character.create(name: "Luke", movie: movies.first)
# db/seeds.rb

# Create sample users
user1 = User.create(name: "John Doe")
user2 = User.create(name: "Jane Smith")

# Create an array of image URLs
image_urls = ['/course/database.png', '/course/web.png']

# Create sample courses
course1 = Course.create(
name: "Course 1",
description: "Description for Course 1",
image: "course1.jpg",
fee: 100.0,
startDate: Date.new(2023, 10, 10)
)
course2 = Course.create(
name: "Course 2",
description: "Description for Course 2",
image: "course2.jpg",
fee: 150.0,
startDate: Date.new(2023, 11, 5)
)
courses = []
6.times do |i|
courses << Course.create(
name: "Course #{i + 1}",
description: "Description for Course #{i + 1}",
image: image_urls[i % 2], # Alternating between the two images
fee: 100.0 + (i * 10), # Incrementing fee
startDate: Date.new(2023, 10, 10) + i.days # Incrementing start date
)
end

# Create sample reservations associated with users and courses
Reservation.create(user_id: user1.id, course_id: course1.id, city: "New York", date: Date.new(2023, 10, 12))
Reservation.create(user_id: user2.id, course_id: course2.id, city: "Los Angeles", date: Date.new(2023, 11, 8))
Reservation.create(user_id: user1.id, course_id: courses[0].id, city: "New York", date: Date.new(2023, 10, 12))
Reservation.create(user_id: user2.id, course_id: courses[1].id, city: "Los Angeles", date: Date.new(2023, 11, 8))

0 comments on commit e207b44

Please sign in to comment.