Skip to content

Instantly share code, notes, and snippets.

@leommoore
Created April 5, 2012 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leommoore/2310174 to your computer and use it in GitHub Desktop.
Save leommoore/2310174 to your computer and use it in GitHub Desktop.
Rails Relationships
#------------------------------------------------------------------------------------------
# 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