Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 21st, 2012  |  syntax: None  |  size: 3.75 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #------------------------------------------------------------------------------------------
  2. #                               One to One Relationship
  3. #------------------------------------------------------------------------------------------
  4. class Subject < ActiveRecord::Base
  5.   has_one :page
  6. end
  7.  
  8. class Page < ActiveRecord::Base
  9.   #Class with belongs_to should have the foreign key
  10.   #belongs_to :subject, {:foreign_key => "subject_id"}
  11.   belongs_to :subject   #foreign key defaults to class_id so it does not need to be specified
  12. end
  13.  
  14. #------------------------------------------------------------------------------------------
  15. #                               One to Many
  16. #------------------------------------------------------------------------------------------
  17. class Subject < ActiveRecord::Base
  18.   has_many :pages
  19. end
  20.  
  21. class Page < ActiveRecord::Base
  22.   belongs_to :subject  
  23.   has_many :sections
  24. end
  25.  
  26. class Section < ActiveRecord::Base
  27.   belongs_to :page  
  28. end
  29.  
  30. #------------------------------------------------------------------------------------------
  31. #                               Many to Many - Simple
  32. #------------------------------------------------------------------------------------------
  33.  
  34. #Need to create a join table
  35. #NB - No primary key column (:id => false)
  36. #Two foreign keys, index both keys together
  37. #By default the table should be named first_table + _ + second_table in alphabetical order
  38.  
  39. class CreateAdminUsersPagesJoin < ActiveRecord::Migration
  40. def change
  41.     create_table :admin_users_pages, :id => false do |t|
  42.         t.integer  :admin_user_id
  43.         t.integer  :page_id
  44.         t.timestamps
  45.     end  
  46.     add_index :admin_users_pages, [:admin_user_id, :page_id]
  47.   end
  48. end
  49.  
  50. #--------------------------------------------------
  51.  
  52. class AdminUser < ActiveRecord::Base
  53.   has_and_belongs_to_many :pages
  54. end
  55.  
  56. class Page < ActiveRecord::Base
  57.   belongs_to :subject  
  58.   has_many :sections
  59.   has_and_belongs_to_many :admin_users
  60.  
  61.   #This can also be done as below, editors may make more sense than admin_users
  62.   #has_and_belongs_to_many :editors, :class_name => "AdminUser"
  63. end
  64.  
  65.  
  66. #------------------------------------------------------------------------------------------
  67. #                               Many to Many - Rich
  68. #------------------------------------------------------------------------------------------
  69.  
  70. #Many to Many Rich requires a model
  71. rails generate model SectionEdit
  72.  
  73. #NB - Needs to have a primary key :id
  74. class CreateSectionEdits < ActiveRecord::Migration
  75. def change
  76.     create_table :sections_edits do |t|
  77.         t.integer  :admin_user_id
  78.         t.integer  :section_id
  79.        
  80.         t.string   :summary
  81.     end  
  82.     add_index :sections_edits, [:admin_user_id, :section_id]
  83.   end
  84. end
  85.  
  86. #--------------------------------------------------
  87.  
  88. class AdminUser < ActiveRecord::Base
  89.   has_and_belongs_to_many :pages
  90.   has_many :section_edits
  91. end
  92.  
  93. class Section < ActiveRecord::Base
  94.   belongs_to :page  
  95.   has_many :section_edits
  96. end
  97.  
  98. class SectionEdit < ActiveRecord::Base
  99.   belongs_to_many :editors, :class_name => "AdminUser", :foreign_key => "admin_user_id"
  100.   # same effect as - belongs_to :admin_user  
  101.   belongs_to :section
  102. end
  103.  
  104. #--------------------------------------------------
  105. # has_many :through - This simplifies the navigation
  106.  
  107. class AdminUser < ActiveRecord::Base
  108.   has_and_belongs_to_many :pages
  109.   has_many :section_edits
  110.   has_many :sections, :through => :section_edits
  111. end
  112.  
  113. class Section < ActiveRecord::Base
  114.   belongs_to :page  
  115.   has_many: section_edits
  116.   has_many :editors, :through => :section_edits, :class_name => "AdminUser"
  117.   #same as - has_many :admin_users, :through => :section_edits
  118. end
  119.  
  120. class SectionEdit < ActiveRecord::Base
  121.   belongs_to_many :editors, :class_name => "AdminUser", :foreign_key => "admin_user_id"
  122.   # same effect as - belongs_to :admin_user  
  123.   belongs_to :section
  124. end