Skip to content

Instantly share code, notes, and snippets.

@JoelCodes
Created May 10, 2017 03:25
Show Gist options
  • Select an option

  • Save JoelCodes/0f38d030d6895e131301dfaa47770235 to your computer and use it in GitHub Desktop.

Select an option

Save JoelCodes/0f38d030d6895e131301dfaa47770235 to your computer and use it in GitHub Desktop.
# http://guides.rubyonrails.org/association_basics.html
require "pry"
require_relative "connection"
ActiveRecord::Schema.define do
drop_table :users if ActiveRecord::Base.connection.data_source_exists? :users
drop_table :groups if ActiveRecord::Base.connection.data_source_exists? :groups
drop_table :hobbies if ActiveRecord::Base.connection.data_source_exists? :hobbies
drop_table :group_memberships if ActiveRecord::Base.connection.data_source_exists? :group_memberships
create_table :users do |t|
t.string :name
t.string :email
end
create_table :groups do |t|
t.string :name
end
create_table :group_memberships do |t|
t.references :user
t.references :group
t.boolean :admin
end
create_table :hobbies do |t|
t.string :name
t.references :user
end
end
class User < ActiveRecord::Base
has_many :hobbies
has_many :group_memberships
has_many :groups, through: :group_memberships
end
class Hobby < ActiveRecord::Base
belongs_to :user
end
class Group < ActiveRecord::Base
has_many :group_memberships
has_many :users, through: :group_memberships
end
class GroupMembership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
joel = User.create! name: 'Joel', email: '[email protected]'
mandolin = joel.hobbies.create! name: 'Mandolin'
p joel.hobbies.to_sql
joel.hobbies.each do |hobby|
p hobby
end
instructors = Group.create! name: 'instructors'
p instructors.users.to_sql
instructors.group_memberships.create! user: joel, admin: true
puts "\nIterating Through Instructors Members"
instructors.users.each do |user|
p user
end
# http://guides.rubyonrails.org/active_record_callbacks.html
require "pry"
require_relative "connection"
ActiveRecord::Schema.define do
drop_table :users if ActiveRecord::Base.connection.data_source_exists? :users
create_table :users do |t|
t.string :name
t.string :email
end
end
class User < ActiveRecord::Base
before_save :downcase_email # Before saving
private
def downcase_email
self.email = email.downcase
end
end
puts "\nCreating Joel with uppercase email"
joel = User.create! name: 'Joel', email: '[email protected]'
p joel.email
require "active_record"
# Output messages from Active Record to standard out
ActiveRecord::Base.logger = Logger.new(STDOUT)
puts 'Establishing connection to database ...'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'ar_demo',
username: 'development',
password: 'development',
host: 'localhost',
port: 5432,
pool: 5,
encoding: 'unicode',
min_messages: 'error'
)
puts 'CONNECTED'
# frozen_string_literal: true
source "https://rubygems.org"
gem "activerecord"
gem "pg"
gem "pry"
# http://guides.rubyonrails.org/active_record_validations.html
require "pry"
require_relative "connection"
ActiveRecord::Schema.define do
drop_table :users if ActiveRecord::Base.connection.data_source_exists?(:users);
drop_table :hobbies if ActiveRecord::Base.connection.data_source_exists?(:hobbies);
create_table :users do |t|
t.string :name
end
create_table :hobbies do |t|
t.references :user
t.string :name
end
end
class User < ActiveRecord::Base
has_many :hobbies
validate :nawar_is_so_mean # Custom validator method
private
def nawar_is_so_mean
if self.name == "Nawar"
errors.add :name, "Dude. That was just. I don't even know, man"
end
end
end
class Hobby < ActiveRecord::Base
belongs_to :user
validates :user, presence: true # Stock validation from ActiveRecord
validate :two_of_the_same_hobby # Custom validator method
private
def two_of_the_same_hobby
if self.user.hobbies.where(name: self.name).count > 0
errors.add :name, "That hobby already exists: #{self.name}"
end
end
end
# This should not create
nawar = User.create name: 'Nawar'
joel = User.create! name: 'Joel'
joel.hobbies.create! name: 'Mandolin'
puts "\n Created 1 Mandolin"
# This should cause an error.
joel.hobbies.create! name: 'Mandolin'
puts "\n Created 2 Mandolins"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment