Skip to content

Instantly share code, notes, and snippets.

@bayendor
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayendor/a920e8f8164f83ee3ea4 to your computer and use it in GitHub Desktop.
Save bayendor/a920e8f8164f83ee3ea4 to your computer and use it in GitHub Desktop.
ActiveRecord Homework for Module 4 of Turing
# Find all the items that have the word "elegant" in it.
items = Item.where("name LIKE ?", "%elegant%")
# Add three orders to a user.
3.times do |i|
user = User.first
order = Order.create!(user_id: user.id)
order.items << Item.find(i + 1)
end
users.orders.create(amount: 5)
# Given an item. Associate it with an order without touching an OrderItem model.
# Create migration to generate the join table
class Item < ActiveRecord::Base
has_and_belongs_to_many :orders # Setup inverse for items model
end
class CreateOrders < ActiveRecord::Migration
def change
add_column :orders, :item_id, :integer # Add foreign key to both sides
end
end
Order.create(amount: 100, user_id: 1, item_id: 1)
# Find the most popular items.
popular = OrderItem.group(:item_id).order('count_id DESC').limit(5).count(:id)
# Find the biggest orders.
biggest_orders = Order.order(amount: :desc).limit(5)
# Find the user who has ordered the most items.
# Find the user who has placed the most orders.
top_buyers = Order.group(:user_id).order('count_id DESC').limit(3).count(:id)
# Find the first ten orders sorted by date created. Don't use Ruby.
orders = Order.order(created_at: :asc).limit(10)
# Get the second ten orders sorted by date created. Don't use Ruby.
orders = Order.order(created_at: :asc).limit(10).offset(10)
# Given a collection of orders, return a collection of the users who those orders belong to, using only one query to the database.
orders = Order.limit(10)
users = orders.group(:user_id)
# Given an item, find all of the users who have ordered that item.
item = Item.first
users = item.orders.pluck(:user_id) # => Array of user_ids
# Get the first ten users and all of their orders in one ActiveRecord query.
users = User.includes(:orders).limit(10)
# Given a collection of items, find all of the users who have ordered that item.
# Find all of the orders created between yesterday and a month ago (you can use rand(1.year.ago..Time.now) in your seed file to spread out the dates a bit).
orders = Order.where("created_at >= :start_date AND created_at <= :end_date",
{start_date: 1.month.ago, end_date: 1.day.ago})
# Set it up so that when you destroy a user, ActiveRecord also destroys all of their orders
class User < ActiveRecord::Base
has_many :orders, dependent: :destroy
end
user = User.first
user.orders.count => 5
user.destroy
# Optional
# Get all of the orders that have more than five items. Don't use Ruby.
Order.all.select { |order| order.items.count > 5 } # Uses Ruby :-(
# On the user model, write a method that finds the user who has the most items in common with that user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment