Giter Site home page Giter Site logo

active_record_basics's Introduction

How many users are there? 50 User.count

What are the 5 most expensive items? Item.order(price: :desc).limit(5)

What's the cheapest book? id: 76, title: "Ergonomic Granite Chair", price: 1496 Item.order(:price).where(category:"Books").limit(1)

Who lives at "6439 Zetta Hills, Willmouth, WY"? Do they have another address? Address.where("street = '6439 Zetta Hills'") Address.where("user_id = '40'")

Correct Virginie Mitchell's address to "New York, NY, 10108". user = Address.joins('JOIN "users" ON "users".id = "addresses".user_id').where("first_name LIKE 'Virginie'")

returns [#<Address:0x007fa57e6f87b0 id: 41, user_id: 39, street: "12263 Jake Crossing", city: "New York", state: "NY", zip: 10108>, #<Address:0x007fa57e6f85a8 id: 42, user_id: 39, street: "83221 Mafalda Canyon", city: "Bahringerland", state: "WY", zip: 24028>]

How much would it cost to buy one of each tool? 46477 Item.where("category like '%tool%'").sum(:price)

How many total items did we sell? 2125 SELECT SUM("orders"."quantity") FROM "orders"

How much was spent on books? 1081352 Item.joins('JOIN "orders" ON "orders".item_id = "items".id').where('category like "%books%"').sum('"items".price * "orders".quantity')

Simulate buying an item by inserting a User for yourself and an Order for that User. User.create(first_name: "Dane", last_name: "Carmichael", email: "[email protected]") Order.create(user_id: 51, item_id: 66, quantity: 6)

active_record_basics's People

Watchers

Alex Carmichael avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.