Rails study questions Flashcards
What part of MVC is Active Record?
M- the model
What is the model layer of the system responsible for?
Representing business data and logic.
What does Active Record facilitate?
The creation and use of business objects whose data requires persistent storage to a database.
What is the Active Record pattern a description of?
An Object Relational Mapping system.
What are two features of objects in Active Record?
Objects carry both persistent dat and behavior which operates on that data.
How will ensuring data access logic as part of the object educate users?
It will educate users on how to write to and read from the database.
What is Object Relational Mapping?
ORM is a technique that connects the rich objects of an application to tables in a relational database management system.
What are two advantages of using ORM?
You don’t have to write SQL statements directly and you need less overall database access code.
What are five mechanisms Active Record gives us?
- Represent models and their data.
- Represent associations between these models.
- Represent inheritance hierarchies through related models.
- Validate models before they get persisted to the database.
- Perform database operations in an object-oriented fashion.
What is a configuration advantage of creating Active Record models?
Explicit configuration is needed only in those cases where you can’t follow the standard convention.
What is optimistic locking?
It allows multiple users to access the same records for edits, and assumes a minimum of conflicts with the data. It does this by checking whether another process has made changes to a record since it was opened.
What does a lock_version column name do?
It adds optimistic locking to a model.
What does a “type” column name do?
Specifies that the model uses Single Table Inheritance.
What is Single Table inheritance?
Inheritance via storing the name of the class in a column that is named “type” by default.
What does an “(association_name)_type” column name do?
Stores the type for polymorphic associations.
What does an “(table_name)_count” column name do?
Used to cache the number of belonging objects on associations.
What does creating an ActiveRecord::Base model allow you to map?
The columns of each row in a database table with the attributes of the instances of your model.
What are three methods you can use to override naming conventions?
self.table_name =
set_fixture_class table_name: ClassName
self.primary_key =
What is the difference between create and new?
The new method will return a new object while create will return the object and save it to the database.
What does CRUD stand for?
Create, Read, Update, Delete
What happens if a block is provided to new or create?
if a block is provided, both create and new will yield the new object to that block for initialization:
What is the method to delete an Active Record object?
destroy
What is a useful method for updating several records in bulk?
update_all
What does validation allow?
Active Record allows you to validate the state of a model before it gets written into the database.
What is the difference between what “save” returns vs. what “save!” returns when Active Record validation fails?
class User < ApplicationRecord validates :name, presence: true end
user = User.new
user. save # => false
user. save! # => ActiveRecord::RecordInvalid: Validation failed: Name can’t be blank
What do Active Record callbacks allow you to do?
Active Record callbacks allow you to attach code to certain events in the life-cycle of your models. This enables you to add behavior to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on
What are migrations?
Rails provides a domain-specific language for managing a database schema called migrations.
Where are migrations stored?
Migrations are stored in files which are executed against any database that Active Record supports using rake.
What does rails keep track of re: migrations?
Rails keeps track of which files have been committed to the database and provides rollback features. To actually create the table, you’d run rails db:migrate and to roll it back, rails db:rollback.
How do migrations work differently on databases that support transactions and those that do not?
On databases that support transactions with statements that change the schema, migrations are wrapped in a transaction. If the database does not support this then when a migration fails the parts of it that succeeded will not be rolled back. You will have to rollback the changes that were made by hand.
What benefit does using a Ruby DSL for migrations provide?
You don’t have to write SQL by hand, allowing your schema and changes to be database independent.
What is a macro?
A single instruction that expands automatically into a set of instructions to perform a particular task.
What is a database transaction?
A sequence of operations performed as a single logical unit of work. A logical unit of work must exhibit four properties, called the atomicity, consistency, isolation and durability (ACID) properties, to qualify as a transaction.
What is an example of using ‘reversible’ to do something that active record doesn’t know how to reverse?
class ChangeProductsPrice < ActiveRecord::Migration[5.0]
def change
reversible do |dir|
change_table :products do |t|
dir.up { t.change :price, :string }
dir.down { t.change :price, :integer }
end
end
end
end
What is an alternative to using reversible?
You can use up and down instead of change.
class ChangeProductsPrice < ActiveRecord::Migration[5.0] def up change_table :products do |t| t.change :price, :string end end
def down change_table :products do |t| t.change :price, :integer end end end
What happens if a migration name is of the form “AddXXXToYYY” or “RemoveXXXFromYYY” and is followed by a list of column names
a migration containing the appropriate add_column and remove_column statements will be created.
$ bin/rails generate migration AddPartNumberToProducts part_number:string
will generate
class AddPartNumberToProducts < ActiveRecord::Migration[5.0]
def change
add_column :products, :part_number, :string
end
end
How do you add an index on a new column from the console?
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
What happens f the migration name is of the form “CreateXXX” and is followed by a list of column names and types
a migration creating the table XXX with the columns listed will be generated.