Skip to content

Instantly share code, notes, and snippets.

View GAierken's full-sized avatar
🦄
Creating.

Gulgina Arkin GAierken

🦄
Creating.
View GitHub Profile
class CreateDefinitions < ActiveRecord::Migration[6.0]
def change
create_table :definitions do |t|
t.references :slang, index: true, foreign_key: true
t.string :language
t.string :meaning
t.timestamps
end
end
@GAierken
GAierken / slang.rb
Last active December 14, 2019 17:59
app/models/slang.rb
class Slang < ApplicationRecord
belongs_to :user
has_many :definitions, dependent: :destroy
has_many :slang_books
has_many :books, through: :slang_books
accepts_nested_attributes_for :definitions, :reject_if => proc {|attributes|
attributes.all? {|k,v| v.blank?}}
validates :phrase, presence: true
validates :origin, presence: true
class Definition < ApplicationRecord
belongs_to :slang
validates :meaning, presence: true
validates :language, presence: true
end
class SlangsController < ApplicationController
before_action :require_login
skip_before_action :require_login, only: [:index, :search, :show]
def index
@slangs = Slang.all
end
def new
@slang = Slang.new
3.times{@slang.definitions.build}
end
<h1>Create New Slang</h1>
<%if flash[:errors]%>
<% flash[:errors].each do |error|%>
<%= error %>
<%end%>
<%end%>
<%= form_for @slang do |f|%>
<%=f.label :phrase%>
create_table "definitions", force: :cascade do |t|
t.integer "slang_id"
t.string "language"
t.string "meaning"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["slang_id"], name: "index_definitions_on_slang_id"
end
@GAierken
GAierken / create_users.rb
Last active February 3, 2020 16:51
self referential relationship
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :password_digest
t.string :username
t.string :address
t.boolean :board_certified
t.timestamps
end
end
@GAierken
GAierken / user.rb
Last active February 3, 2020 17:00
class User < ApplicationRecord
has_many :provider_appointments, foreign_key: :client_id, class_name: "Appointment"
has_many :providers, through: :provider_appointments
has_many :client_appointments, foreign_key: :provider_id, class_name: "Appointment"
has_many :clients, through: :client_appointments
end
class Appointment < ApplicationRecord
belongs_to :client, class_name: "User"
belongs_to :provider, class_name: "User"
end
ActiveRecord::Schema.define(version: 2020_01_30_155454) do
enable_extension "plpgsql"
create_table "appointments", force: :cascade do |t|
t.integer "client_id"
t.integer "provider_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "password_digest"