Created
April 5, 2012 11:44
-
-
Save leommoore/2310174 to your computer and use it in GitHub Desktop.
Rails Relationships
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------------------ | |
# One to One Relationship | |
#------------------------------------------------------------------------------------------ | |
class Subject < ActiveRecord::Base | |
has_one :page | |
end | |
class Page < ActiveRecord::Base | |
#Class with belongs_to should have the foreign key | |
#belongs_to :subject, {:foreign_key => "subject_id"} | |
belongs_to :subject #foreign key defaults to class_id so it does not need to be specified | |
end | |
#------------------------------------------------------------------------------------------ | |
# One to Many | |
#------------------------------------------------------------------------------------------ | |
class Subject < ActiveRecord::Base | |
has_many :pages | |
end | |
class Page < ActiveRecord::Base | |
belongs_to :subject | |
has_many :sections | |
end | |
class Section < ActiveRecord::Base | |
belongs_to :page | |
end | |
#------------------------------------------------------------------------------------------ | |
# Many to Many - Simple | |
#------------------------------------------------------------------------------------------ | |
#Need to create a join table | |
#NB - No primary key column (:id => false) | |
#Two foreign keys, index both keys together | |
#By default the table should be named first_table + _ + second_table in alphabetical order | |
class CreateAdminUsersPagesJoin < ActiveRecord::Migration | |
def change | |
create_table :admin_users_pages, :id => false do |t| | |
t.integer :admin_user_id | |
t.integer :page_id | |
t.timestamps | |
end | |
add_index :admin_users_pages, [:admin_user_id, :page_id] | |
end | |
end | |
#-------------------------------------------------- | |
class AdminUser < ActiveRecord::Base | |
has_and_belongs_to_many :pages | |
end | |
class Page < ActiveRecord::Base | |
belongs_to :subject | |
has_many :sections | |
has_and_belongs_to_many :admin_users | |
#This can also be done as below, editors may make more sense than admin_users | |
#has_and_belongs_to_many :editors, :class_name => "AdminUser" | |
end | |
#------------------------------------------------------------------------------------------ | |
# Many to Many - Rich | |
#------------------------------------------------------------------------------------------ | |
#Many to Many Rich requires a model | |
rails generate model SectionEdit | |
#NB - Needs to have a primary key :id | |
class CreateSectionEdits < ActiveRecord::Migration | |
def change | |
create_table :sections_edits do |t| | |
t.integer :admin_user_id | |
t.integer :section_id | |
t.string :summary | |
end | |
add_index :sections_edits, [:admin_user_id, :section_id] | |
end | |
end | |
#-------------------------------------------------- | |
class AdminUser < ActiveRecord::Base | |
has_and_belongs_to_many :pages | |
has_many :section_edits | |
end | |
class Section < ActiveRecord::Base | |
belongs_to :page | |
has_many :section_edits | |
end | |
class SectionEdit < ActiveRecord::Base | |
belongs_to_many :editors, :class_name => "AdminUser", :foreign_key => "admin_user_id" | |
# same effect as - belongs_to :admin_user | |
belongs_to :section | |
end | |
#-------------------------------------------------- | |
# has_many :through - This simplifies the navigation | |
class AdminUser < ActiveRecord::Base | |
has_and_belongs_to_many :pages | |
has_many :section_edits | |
has_many :sections, :through => :section_edits | |
end | |
class Section < ActiveRecord::Base | |
belongs_to :page | |
has_many: section_edits | |
has_many :editors, :through => :section_edits, :class_name => "AdminUser" | |
#same as - has_many :admin_users, :through => :section_edits | |
end | |
class SectionEdit < ActiveRecord::Base | |
belongs_to_many :editors, :class_name => "AdminUser", :foreign_key => "admin_user_id" | |
# same effect as - belongs_to :admin_user | |
belongs_to :section | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment