| Orders | _____________ | - id | | Customers | | - customer_id | -----------> | - id |
class Order < ActiveRecord::Base
belongs_to :customer
end
order = Order.first
order.customer
=> returns the customer for order
| Orders | _____________ | - id | | Customers | | - customer_id | <----------- | - id |
class Order < ActiveRecord::Base
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_many :orders
end
customer = Customer.first
customer.orders
=> returns the orders for customer
class Order < ActiveRecord::Base
belongs_to :customer, inverse_of: :orders
end
class Customer < ActiveRecord::Base
has_many :orders, inverse_of: :customer
end
class CreateOrders < ActiveRecord::Migration
def change
create_table :customers do |t|
t.string :name
t.timestamps null: false
end
create_table :orders do |t|
t.belongs_to :customer, index: true
t.datetime :order_date
t.timestamps null: false
end
end
end
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.integer :customer_id
t.datetime :order_date
t.timestamps null: false
end
add_index :orders, :customer_id
end
end
| OrderItem | _________________ | - id | | Order | _____________ | - order_id | -----------> | - id | | Customer | | - product_id | | - customer_id | -----------> | - id |
class OrderItem < ActiveRecord::Base
belongs_to :order
end
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_items
end
class Customer < ActiveRecord::Base
has_many :orders
end
customer = Customer.first
customer.orders.first.order_items
=> returns order items for first order
customer.orders.map(&:order_items)
=> returns array of all order items for customer
order_item = OrderItem.first
order_item.order.customer
=> Returns customer for the order_item
class OrderItem < ActiveRecord::Base
belongs_to :order
def customer
order.custoemr
end
end
OrderItem.first.customer
=> returns customer for the order_item
class OrderItem < ActiveRecord::Base
delegate :customer, to: :order
belongs_to :order
end
OrderItem.first.customer
=> returns customer for the order_item
| OrderItem | _________________ | - id | | Order | _____________ | - order_id | <----------- | - id | | Customer | | - product_id | | - customer_id | <----------- | - id |
class OrderItem < ActiveRecord::Base
delegate :customer, to: :order
belongs_to :order
end
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_items
end
class Customer < ActiveRecord::Base
has_many :orders
has_many :order_items, through: :orders
end
customer = Customer.first
customer.order_items
=> returns all customer order items... same as
customer.orders.map(&:order_items)
=> and
customer = Customer.first
customer.orders.first.order_items
=> still returns order items for an order
| Users | ____ ___________________ | - id | \ | AddressesUsers | ------------- -----> | - user_id | _____________ /-----> | - address_id | | Addresses | / ------------------- | - id | ___/
class User < ActiveRecord::Base
has_and_belongs_to_many :addresses
end
class Address < ActiveRecord::Base
has_and_belongs_to_many :users
end
Address.first.users
=> returns all users at an address
User.first.addresses
=> returns all addresses for a user
class CreateUsersAndAddresses < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps null: false
end
create_table :addresses do |t|
t.string :street
t.timestamps null: false
end
create_table :addresses_users, id: false do |t|
t.belongs_to :user, index: true
t.belongs_to :address, index: true
end
end
end
| Users | ____ _________________ | - id | \ | AddressUsers | ------------- -----> | - user_id | _____________ /-----> | - address_id | | Addresses | / ----------------- | - id | ___/
class User < ActiveRecord::Base
has_many :address_users
has_many :addresses, through: :address_users
end
class Address < ActiveRecord::Base
has_many :address_users
has_many :users, through: :address_users
end
class AddressUser
belongs_to :user
belongs_to :address
end
...works the same way
Address.first.users
=> returns all users at an address
User.first.addresses
=> returns all addresses for a user
class CreateUsersAndAddresses < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps null: false
end
create_table :addresses do |t|
t.string :street
t.timestamps null: false
end
create_table :addresses_users, id: false do |t|
t.belongs_to :user, index: true
t.belongs_to :address, index: true
end
end
end
- has_one
- has_one :through
- polymorphic
- self joins
But we'll cover those later
Do has any?