Skip to content
Permalink
Browse files

Relational dbs in Rails

  • Loading branch information
ImsirovicAjdin committed Dec 27, 2019
1 parent 0782e91 commit 050ea8567026247692621a804ce297d4672d87bd
@@ -1,6 +1,8 @@
class Movie < ApplicationRecord
# self.primary_key = 'id'

has_many :users

scope :from_newest, lambda { order("created_at DESC") }
scope :lookup, lambda {|titlestring| where(["title LIKE ?", "%#{titlestring}%"]) }

@@ -0,0 +1,3 @@
class User < ApplicationRecord
belongs_to :movie, {:optional => true} #, { :foreign_key => 'movie_id' }, dependent: :destroy # on_delete: :cascade
end
@@ -0,0 +1,6 @@
class AddStatusToMovies < ActiveRecord::Migration[6.0]
def change
# add_column(table, column, type, options)
add_column(:movies, :status, :string)
end
end
@@ -0,0 +1,6 @@
class RemoveStatusFromMovies < ActiveRecord::Migration[6.0]
def change
# remove_column(table, column)
remove_column(:movies, :status)
end
end
@@ -0,0 +1,13 @@
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :first
t.string :last
t.string :username
t.string :email
t.string :status

t.timestamps
end
end
end
@@ -0,0 +1,9 @@
class AddForeignKey < ActiveRecord::Migration[6.0]
def change
# add_column(table, column, type, options)
add_column(:users, :movie_id, :bigint)

# add_foreign_key(from_table, to_table, options = {})
add_foreign_key(:users, :movies)
end
end
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_24_215938) do
ActiveRecord::Schema.define(version: 2019_12_26_155147) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -23,4 +23,16 @@
t.datetime "updated_at", precision: 6, null: false
end

create_table "users", force: :cascade do |t|
t.string "first"
t.string "last"
t.string "username"
t.string "email"
t.string "status"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "movie_id"
end

add_foreign_key "users", "movies"
end
@@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
first: MyString
last: MyString
username: MyString
email: MyString
status: MyString

two:
first: MyString
last: MyString
username: MyString
email: MyString
status: MyString
@@ -0,0 +1,7 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 050ea85

Please sign in to comment.
You can’t perform that action at this time.