Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An introduction to ActiveRecord Associations - ...

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

An introduction to ActiveRecord Associations - Training Session

Avatar for Shaiju Edakulangara

Shaiju Edakulangara

July 07, 2015

More Decks by Shaiju Edakulangara

Other Decks in Technology

Transcript

  1. Take away from this session Active Record Associations Why Associations?

    The belongs_to Association The has_one Association The has_many Association The has_many :through Association The has_and_belongs_to_many Association Polymorphic Associations
  2. Active Record Associations In Rails, an association is a connection

    between two Active Record models Associations are implemented using macro-style calls Rails supports six types of associations 1. belongs_to 2. has_one 3. has_many 4. has_many :through 5. has_one :through 6. has_and_belongs_to_many
  3. Why Associations? Without associations class Customer < ActiveRecord::Base end class

    Order < ActiveRecord::Base end To add a new order for an existing customer order = Order.create(order_date: Time.now, customer_id: customer.id) To delete a customer orders = Order.where(customer_id: customer.id) orders.each do |order| order.destroy end customer.destroy
  4. Why Associations? With associations class Customer < ActiveRecord::Base has_many :orders,

    dependent: :destroy end class Order < ActiveRecord::Base belongs_to :customer end To add a new order for an existing customer order = customer.orders.create(order_date: Time.now) To delete a customer customer.destroy To fetch all order of a customer customer.orders To fetch customer details of a order order.customer
  5. The belongs_to Association A belongs_to association sets up a one-to-one

    connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model
  6. class Order < ActiveRecord::Base belongs_to :customer end The belongs_to Association

    class CreateOrders < ActiveRecord::Migration def change create_table :orders do |t| t.belongs_to :customer, index: true t.datetime :order_date t.timestamps null: false end end end OR class CreateOrders < ActiveRecord::Migration def change create_table :orders do |t| t.integer :customer_id, index: true t.datetime :order_date t.timestamps null: false end end end Another way to add reference
  7. The has_one Association A has_one association also sets up a

    one-to-one connection with another model, but with somewhat different semantics (and consequences).
  8. The has_one Association class Supplier < ActiveRecord::Base has_one :account end

    class CreateAccounts < ActiveRecord::Migration def change create_table :accounts do |t| t.belongs_to :supplier, index: true t.string :account_number t.timestamps null: false end end end class CreateAccounts < ActiveRecord::Migration def change create_table :accounts do |t| t.integer :supplier_id, index: true t.string :account_number t.timestamps null: false end end end
  9. The has_many Association class Customer < ActiveRecord::Base has_many :orders end

    class CreateOrders < ActiveRecord::Migration def change create_table :orders do |t| t.belongs_to :customer, index:true t.datetime :order_date t.timestamps null: false end end end
  10. 1 The has_many :through Association A has_many :through association indicates

    that the declaring model can be matched with zero or more instances of another model by proceeding through a third model
  11. 1 The has_many :through Association class Physician < ActiveRecord::Base has_many

    :appointments has_many :patients, through: :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient < ActiveRecord::Base has_many :appointments has_many :physicians, through: :appointments end class CreateAppointments < ActiveRecord::Migration def change create_table :appointments do |t| t.belongs_to :physician, index: true t.belongs_to :patient, index: true t.datetime :appointment_date t.timestamps null: false end end end
  12. 1 1 The has_and_belongs_to_many Association A has_and_belongs_to_many association creates a

    direct many-to-many connection with another model, with no intervening model.
  13. 1 The has_and_belongs_to_many Association class CreateAssemblies < ActiveRecord::Migration def change

    create_table :assemblies do |t| t.string :name t.timestamps null: false end end end class CreateParts < ActiveRecord::Migration def change create_table :parts do |t| t.string :part_number t.timestamps null: false end end end class CreateAssembliesAndParts < ActiveRecord::Migration def change create_table :assemblies_parts, id: false do |t| t.belongs_to :assembly, index: true t.belongs_to :part, index: true end end end
  14. 1 1 Polymorphic Associations With polymorphic associations, a model can

    belong to more than one other model, on a single association
  15. 1 Polymorphic Associations class Picture < ActiveRecord::Base belongs_to :imageable, polymorphic:

    true end class Employee < ActiveRecord::Base has_many :pictures, as: :imageable end class Product < ActiveRecord::Base has_many :pictures, as: :imageable end class CreatePictures < ActiveRecord::Migration def change create_table :pictures do |t| t.string :name t.integer :imageable_id t.string :imageable_type t.timestamps null: false end add_index :pictures, :imageable_id end end class CreatePictures < ActiveRecord::Migration def change create_table :pictures do |t| t.string :name t.references :imageable, polymorphic: true, index: true t.timestamps null: false end end end