SlideShare a Scribd company logo
1 of 42
80


60


40


20


 0
  2007   2008   2009
Po w ere d by   vs
class Employee < ActiveRecord::Base
  validates_presence_of :name


CREATE TABLE employees (
  name VARCHAR(40) NOT NULL,
class Employee < ActiveRecord::Base
  validates_length_of :login,
                      :minimum => 5


CREATE TABLE employees (
  login VARCHAR(10)
    check(length(login) > 4),
begin
  emp = Employee.new(:login => '')
  emp.save_with_validation(false)
  fail
rescue ActveRecord::StatementInvalid
end
Departments   Employees
class Employee < ActiveRecord::Base
  belongs_to :department
  validates_presence_of :department


CREATE TABLE employees (
  department_id INTEGER NOT NULL
    REFERENCES departments(id),
Companies   Departments   Employees
has_many :though

company.employees.size


SELECT count(*) AS count_all
FROM employees
INNER JOIN departments
   ON employees.department_id =
      departments.id
WHERE departments.company_id = 1
SELECT count(*) AS count_all
FROM employees
WHERE company_id = 1



Companies


                ?
               Departments     Employees
company_id    department_id




Companies         Departments     Employees
id                id              id
                  company_id      company_id
                                  department_id
Companies   Departments     Employees
id          company_id      id
            department_id   company_id
                            department_id
departments

CREATE TABLE departments(
  company_id INTEGER NOT NULL
    REFERENCES companies(id),
  department_id SERIAL NOT NULL,

 PRIMARY KEY (company_id,
              department_id)
employees

CREATE TABLE employees(
  company_id INTEGER NOT NULL
     REFERENCES companies(id),
  department_id INTEGER NOT NULL,
  FOREIGN KEY
    (company_id, department_id)
    REFERENCES department
      (company_id, department_id)
departments                       company_id
 department_id                   employees


 Companies       Departments       Employees
 id              company_id        id
                 department_id     company_id
                                   department_id
# gem install composite_primary_keys
Department

class Department < ActiveRecord::Base
  set_primary_keys :company_id,
                   :department_id

 has_many :employees,
          :foreign_keys =>
            [:company_id,
             :department_id]
Company

class Company < ActiveRecord::Base
  def employees_size
    Employee.count(
      :conditions =>
        ['company_id = ?', id])
  end
CREATE VIEW big_campanies AS
SELECT * FROM companies WHERE id IN
  (SELECT company_id FROM
    (SELECT compay_id, count(id) AS c
            FROM employees
            GROUP BY company_id) AS t
    WEHRE c > 1000)
BigCampany




class BigCampany < ActiveRecord::Base
  has_many :department
end
SELECT * FROM (SELCT * FROM ...)
ActiveRecord associations with composite primary keys
ActiveRecord associations with composite primary keys

More Related Content

Similar to ActiveRecord associations with composite primary keys

Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Finalmukesh24pandey
 
Build .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and DashboardBuild .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and DashboardIron Speed
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSgaurav koriya
 
Constraint Optimization
Constraint OptimizationConstraint Optimization
Constraint OptimizationOren Nakdimon
 
Sql task answers
Sql task answersSql task answers
Sql task answersNawaz Sk
 
Reporting aggregated data using the group functions
Reporting aggregated data using the group functionsReporting aggregated data using the group functions
Reporting aggregated data using the group functionsSyed Zaid Irshad
 
Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)Tâm
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answersNawaz Sk
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Unidad 4 actividad 1
Unidad 4 actividad 1Unidad 4 actividad 1
Unidad 4 actividad 1KARY
 

Similar to ActiveRecord associations with composite primary keys (20)

Les20
Les20Les20
Les20
 
Les04
Les04Les04
Les04
 
BIS05 Introduction to SQL
BIS05 Introduction to SQLBIS05 Introduction to SQL
BIS05 Introduction to SQL
 
Les04
Les04Les04
Les04
 
Les05
Les05Les05
Les05
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
base de datos ejemplo
base de datos ejemplo base de datos ejemplo
base de datos ejemplo
 
sql language
sql languagesql language
sql language
 
Build .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and DashboardBuild .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and Dashboard
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
 
Constraint Optimization
Constraint OptimizationConstraint Optimization
Constraint Optimization
 
Les08
Les08Les08
Les08
 
Sql task answers
Sql task answersSql task answers
Sql task answers
 
Reporting aggregated data using the group functions
Reporting aggregated data using the group functionsReporting aggregated data using the group functions
Reporting aggregated data using the group functions
 
Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Unidad 4 actividad 1
Unidad 4 actividad 1Unidad 4 actividad 1
Unidad 4 actividad 1
 
Les18
Les18Les18
Les18
 

More from Naoto Takai

私と自分戦略
私と自分戦略私と自分戦略
私と自分戦略Naoto Takai
 
JRubyによるエンタープライズweb開発
JRubyによるエンタープライズweb開発JRubyによるエンタープライズweb開発
JRubyによるエンタープライズweb開発Naoto Takai
 
地域.rbのご提案
地域.rbのご提案地域.rbのご提案
地域.rbのご提案Naoto Takai
 
JRuby最新事情@札幌
JRuby最新事情@札幌JRuby最新事情@札幌
JRuby最新事情@札幌Naoto Takai
 
JRoRでGAE/Jの手引き
JRoRでGAE/Jの手引きJRoRでGAE/Jの手引き
JRoRでGAE/Jの手引きNaoto Takai
 
5分でわかるJRuby最新事情
5分でわかるJRuby最新事情5分でわかるJRuby最新事情
5分でわかるJRuby最新事情Naoto Takai
 
JRuby on Rails and Thread Safety
JRuby on Rails and Thread SafetyJRuby on Rails and Thread Safety
JRuby on Rails and Thread SafetyNaoto Takai
 

More from Naoto Takai (7)

私と自分戦略
私と自分戦略私と自分戦略
私と自分戦略
 
JRubyによるエンタープライズweb開発
JRubyによるエンタープライズweb開発JRubyによるエンタープライズweb開発
JRubyによるエンタープライズweb開発
 
地域.rbのご提案
地域.rbのご提案地域.rbのご提案
地域.rbのご提案
 
JRuby最新事情@札幌
JRuby最新事情@札幌JRuby最新事情@札幌
JRuby最新事情@札幌
 
JRoRでGAE/Jの手引き
JRoRでGAE/Jの手引きJRoRでGAE/Jの手引き
JRoRでGAE/Jの手引き
 
5分でわかるJRuby最新事情
5分でわかるJRuby最新事情5分でわかるJRuby最新事情
5分でわかるJRuby最新事情
 
JRuby on Rails and Thread Safety
JRuby on Rails and Thread SafetyJRuby on Rails and Thread Safety
JRuby on Rails and Thread Safety
 

Recently uploaded

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

ActiveRecord associations with composite primary keys

  • 1.
  • 2.
  • 3. 80 60 40 20 0 2007 2008 2009
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. Po w ere d by vs
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. class Employee < ActiveRecord::Base validates_presence_of :name CREATE TABLE employees ( name VARCHAR(40) NOT NULL,
  • 18. class Employee < ActiveRecord::Base validates_length_of :login, :minimum => 5 CREATE TABLE employees ( login VARCHAR(10) check(length(login) > 4),
  • 19. begin emp = Employee.new(:login => '') emp.save_with_validation(false) fail rescue ActveRecord::StatementInvalid end
  • 20. Departments Employees
  • 21. class Employee < ActiveRecord::Base belongs_to :department validates_presence_of :department CREATE TABLE employees ( department_id INTEGER NOT NULL REFERENCES departments(id),
  • 22.
  • 23.
  • 24. Companies Departments Employees
  • 25. has_many :though company.employees.size SELECT count(*) AS count_all FROM employees INNER JOIN departments ON employees.department_id = departments.id WHERE departments.company_id = 1
  • 26. SELECT count(*) AS count_all FROM employees WHERE company_id = 1 Companies ? Departments Employees
  • 27. company_id department_id Companies Departments Employees id id id company_id company_id department_id
  • 28. Companies Departments Employees id company_id id department_id company_id department_id
  • 29. departments CREATE TABLE departments( company_id INTEGER NOT NULL REFERENCES companies(id), department_id SERIAL NOT NULL, PRIMARY KEY (company_id, department_id)
  • 30. employees CREATE TABLE employees( company_id INTEGER NOT NULL REFERENCES companies(id), department_id INTEGER NOT NULL, FOREIGN KEY (company_id, department_id) REFERENCES department (company_id, department_id)
  • 31. departments company_id department_id employees Companies Departments Employees id company_id id department_id company_id department_id
  • 32. # gem install composite_primary_keys
  • 33. Department class Department < ActiveRecord::Base set_primary_keys :company_id, :department_id has_many :employees, :foreign_keys => [:company_id, :department_id]
  • 34. Company class Company < ActiveRecord::Base def employees_size Employee.count( :conditions => ['company_id = ?', id]) end
  • 35.
  • 36.
  • 37.
  • 38. CREATE VIEW big_campanies AS SELECT * FROM companies WHERE id IN (SELECT company_id FROM (SELECT compay_id, count(id) AS c FROM employees GROUP BY company_id) AS t WEHRE c > 1000)
  • 39. BigCampany class BigCampany < ActiveRecord::Base has_many :department end
  • 40. SELECT * FROM (SELCT * FROM ...)