Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created April 13, 2017 13:40
Show Gist options
  • Save JackHowa/2fa14c916492ec91e231da9733294b26 to your computer and use it in GitHub Desktop.
Save JackHowa/2fa14c916492ec91e231da9733294b26 to your computer and use it in GitHub Desktop.
active records retrieve commands - week 3
$ bundle install
$ bundle exec rake db:create
$ bundle exec rake db:migrate
$ bundle exec rake db:seed
bundle exec rake console
Dog.all
The SQL executed is equivalent to SELECT "dogs".* FROM "dogs" WHERE "dogs"."age" = 1.
Dog.where("age = ? and name like ?", 1, '%Te%')
equivalent of where in sql
Dog.order(age: :desc)
descending, table name is dog
age is the attribute
Dog.limit(2)
returns active record objects
equivalent of count Dog.count
Dog.pluck(:name, :age)
selecting two attributes or columns
pluck is just two fields
Dog.first
just the first object in the table
Dog.find(3)
basically an id of 3
Dog.find [1, 3]
finding two ids
Dog.find_by(name: "Jayda")
find is diff from where bc where outputs a collection of objects
whereas find_by only returns one - and the first one
Dog.find_by(name: "Poop") outputs nil
Dog.order(name: :asc).where(age: 1).limit(1)
chaining methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment