w3d3 rails Flashcards
How do you run migrations
bundle exec rails db:migrate
How do you create a database with rails
set up config/database.yml
bundle exec rails db:create
What are 5 gems to add to rails for development
annotate better_errors binding_of_caller byebug pry-rails
How do you run migrations?
rails db:migrate
Where can you see a copy of your database’s current schema as reported by rails?
db/schema.rb
What’s the best/fastest way to initialize a new database? What does this rely on?
rails db:schema:load
db/schema.rb
How do you enforce that a column must have a value?
within create_table:
t.string :name, null: false
What’s the difference between belongs_to and has_many ?
What do they have in common?
belongs_to exists on the model that contains the primary key
has_many exists on the other model
They allow the use of helper instance methods
What arguments are passed to a belongs_to or has_many association?
(trick) answer: two
better answer: we use ruby’s ability to use a hash as a final argument without braces to pass several options to the association method like so:
belongs_to :table_name, primary_key: :id,
foreign_key: :foreign_key_id,
class_name: ‘SomeClass’
example:
belongs_to :house,
brimary_key: :id,
foreign_key: :house_id,
class_name: ‘House’
What arguments are passed to a belongs_to or has_many association?
(trick) answer: two
better answer: we use ruby’s ability to use a hash as a final argument without braces to pass several options to the association method like so:
belongs_to :table_name, primary_key: :id,
foreign_key: :foreign_key_id,
class_name: ‘SomeClass’
example:
belongs_to :house,
brimary_key: :id,
foreign_key: :house_id,
class_name: ‘House’
What does this do:
validates :attribute, presence: true
It validates that a particular attribute exists on a method when we call #save
How do we validate the uniqueness of an attribute for an object?
Add this to a validates macro:
uniqueness: true
How do we create a custom validator for a model?
- create a method that conditionally shovels an error message into a new key on the self.errors hash
- add this line:
validate :my_validator_method_name
How do we see the errors for an object?
errors.full_messages
What’s the difference between ApplicationModel#save and #save!
save will return false if there are errors.
What’s the difference between ApplicationModel#save and #save!
save will return false if there are errors.
How would you use a symbol as a value in a SQL string?
awesome_symbol: 55
<
What are two levels of data integrity-protection in rails?
DB-level constraints
Model-level validations
Why shouldn’t you roll back a migration?
Can make things out-of-sync with other people working on the same database.
Impossible to keep consistent with a production database.
How do you get to a sql console in rails?
rails db
What is the superclass for all rails models?
ApplicationRecord
What is ApplicationRecord
The superclass that all rails models inherit from.
When does code directly inside a class definition run?
When the class is first parsed.
How do you recreate your db from scratch? What steps does this take?
rails db:reset
- drops entire database
- runs db:migrate
- runs db:seed
What arguments are passed to has_many? How many of these are optional?
has_many :relation_name,
class_name: __,
foreign_key: __,
primary_key: __
All but the relation_name are optional
What’s the difference between has_one and has_many?
has_one is just a has_many with a call to Array#first on the result
How does a “has_many through” association know which association to go through?
What does this do in SQL?
through: :which_association
joins the model’s table to the through: table
How do you search for files in Atom?
cmd + T
How do you center a div?
margin 0 auto
Why do inline-block elements have spacing between them?
How do you fix this?
They’re treated like words. The “\n” after a tag is interpreted as whitespace.
To fix: float one element to align to the other, or set text-size: 0
SQL: How do you specify an INTEGER to auto-increment?
SERIAL
How do you list all dbs via psql?
\l (lowercase L)
How do you escape quotes in SQL
double the quote
How do you do emojis in Atom
cmd + ctrl + space
What’s the default join type?
INNER JOIN
How do you validate that a model have a unique name?
validate :name, uniqueness: true
What will this do:
validate :name, uniqueness: true
Validate that a model has a unique name
How do you validate that a model contain a particular attribute?
validate :attribute_name, presence: true
What will this do:
validate :attribute_name, presence: true
validate that a model contains a particular attribute
How do you create an optional belongs_to association?
pass in this parameter to belongs_to
optional: true
How do we skip a validation if an object is nil? How about if it is empty?
pass in this parameter to validates :
allow_nil: true
allow_blank: true
How do you optionally specify a validation?
pass in this parameter to validates :
if: :my_boolean_method?
What’s the major difference between an ActiveRecord::Relation and an array?
The former is lazy. The contents are not fetched until needed.
What are two ways of forcing the evaluation of a Relation?
to_a
Relation#load
In relation to queries within ActiveRecord::Base, what is a scope?
Why is it called a scope?
An ActiveRecord::Base class method that returns all or part of a query and then returns the resulting Relation object
It’s called a scope because we can tack more query method onto the end of it. It effectively “scopes” the space in which we’re querying data from.
Can we use a scope from an instance? Why or why isn’t this surprising?
Yes, we can.
It’s surprising because we define it in a class method but it can be used as an instance method.
What’s the difference between ::find and ::find_by ?
::find accepts a single argument, the id of the record in question, and raises an exception if the record is not found.
::find_by accepts an options hash, which allows for more detailed search criteria, and returns nil if the record is not found.
What query method can you use to retrieve records in a specific order?
::order
How do you use your own SQL to locate a record?
::find_by_sql
How does rails deal with the SQL ternary problem? What is the SQL ternary problem?
Comparing something with a NULL object always returns NULL.
rails uses the ::where method to correctly specify one of these two comparisons:
obj = ?
obj IS NULL
How would you run a rails query for a User that is not NULL?
User.where.not(name: nil)
How do you set a column to be required within a migration?
add this to the end of the t.coltype line:
null: false
ex:
t.text “body”, null: false
How do you prefetch associations for a model?
includes(:association_name)
includes(:association_name)
What does this code do?
Prefetches objects associated with an association
What’s an N+1 query? Why is it bad? How can you get around it?
It means you run N additional queries when fetching multiple objects from the same relation.
This is expensive in DB overhead.
You can get around it by prefetching data from the association using #includes
CSS: What is a clearfix?
An element that has the following properties for the purpose of containing a float (using the dimensions of the float object to dictate the dimensions of its parent):
.clearfix:after { content: ""; display: block; clear: both; }
How do you debug queries generated by rails?
add .to_sql on the end of a query