dnestoff
9/9/2016 - 1:02 AM

Rails: Associations (Active Record)

The different associations in Active Record models

(see: https://github.com/mattbaker/association-reporter)

#Must Watch --- [DevBootcamp Talk: Active Record Associations Live Coding](https://talks.devbootcamp.com/2014-bluegills-aeu-a-active-record-associations-live-coding-aeu-a-t-shirt-challenge)

##### "When questioning associations => ONLY WRITE THE CODE YOU NEED ####

### BELONGS_TO

class Rating < ActiveRecord::Base
  belongs_to :dog
end

class Rating < ActiveRecord::Base
  belongs_to :judge, { :class_name => "Person", :foreign_key => :judge_id }
end

### HAS_MANY

class Dog < ActiveRecord::Base
  has_many :ratings
end

class Person < ActiveRecord::Base
  has_many :dogs, { :class_name => "Dog", :foreign_key => :owner_id }
end

# For those that don't defy convention 
# we just need to make sure that our table is singular. 

# Some associations do defy the following two conventions:
  # - Active Record expects to find a class with a name matching the first argument passed to `.belongs_to`.  So if we pass `:dog`, Active Record expects to find a `Dog` class.
  # - Active Record expects to find a foreign key field with a name matching the first argument passed to `.belongs_to`.  If we pass the table `:dog`, so Active Record expects to find a foreign key field `dog_id` on our table.

#WHEN ASSOCIATIONS DEFY CONVENTION

belongs_to :orange_tree, { :class_name => "Class", :foreign_key => :orange_tree_id }
has_many :oranges, { :class_name => "Class", :foreign_key => :orange_tree_id }

# When we break convention, we have to tell Active Record which class and/or foreign key field to use. Note: A comma follows the table name given after `#belongs_to`.

###################################################################

#HAS_MANY THROUGH

## has many can be used to make associations through JOIN tables (MANY-TO-MANY)
#when defying convention, has_many through uses "source:"

class Booking < ActiveRecord::Base
  belongs_to :guest, :class_name => "User", foreign_key: :guest_id
  belongs_to :room
  has_one :hotel, through: :room
end

class Hotel < ActiveRecord::Base
  has_many :rooms
  has_many :bookings, through: :rooms
    #the below has_many requires source since booked_guest is not expected convention of guest_id
  has_many :booked_guests, through: :bookings, source: :guest
end

class Room < ActiveRecord::Base
  has_many :bookings
  belongs_to :hotel
end

class User < ActiveRecord::Base
  has_many :bookings, foreign_key: :guest_id
  has_many :booked_rooms, through: :bookings, source: :room
  has_many :booked_hotels, through: :booked_rooms, source: :hotel
end
class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
end

# #######################

class Article < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class Photo < ActiveRecord::Base
  has_many :comments, :as => :commentable
  #...
end

class Event < ActiveRecord::Base
  has_many :comments, :as => :commentable
end