OOP Flashcards

1
Q

Write the migration to rename a column in a table?

A

class RenameOldColumnInTable < ActiveRecord::Migration
def change
rename_column :table, :old_column_name, :new_column
end
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a database query?

A

A DB query is a connection with the DB to read, create, update or delete records.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Complete the following model with a presence and uniqueness validation on :github_nickname

A

Class Developer < ActiveRecord::Base
validates :github_nickname, presence: true, uniqueness: true
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What is the SQL query generated by destroy
------------------
doctor.id
# =>  1
doctor.destroy
A

This deletes one record in the table doctors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you run a pending migrations?

A

$ rake db:migrate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you modelize a n:n relation between the following models:

class Post < ActiveRecord::Base
end
class Category < ActiveRecord::Base
end
A

class CreateCategoriesPosts < ActiveRecord::Migration
create_table :categories_posts do |t|
t.references :post, index: true
t.references :category, index: true
t.timestamps null: false
end
end
———————
And using through in your associations instructions in your models classes:
———————–
class Post < ActiveRecord::Base
has_many :categories_posts
has_many :categories, through: :categories_posts
end

class Category < ActiveRecord::Base
has_many :categories_posts
has_many :posts, through: :categories_posts
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we add a a column to a table thanks to a migration?

A

class AddNewColumntoTable < ActiveRecord::Migration
def change
add_column :table, :new_column, :format
end
end
———————
If you name properly your migration class AddColumnToTable, Rails will autocomplete the code inside the change method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write the migration to add an intern_id foreign key in patients table

A

class AddInternReferenceToPatiens < ActiveRecord::Migration
def change
add_reference :patiens, :intern, index: true
end
end
——————
Note the second argument :intern and not :intern_id

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

SELECT * FROM doctors WHERE specialty LIKE “%surgery”;

A

This query returns all doctors having specialty ending with “surgery”(note the %)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What symbol is used in a SQL query to get all columns?

A

SELECT * FROM TABLE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the SQL keyword to join two tables in a query?

A

JOIN…ON

SELECT * FROM first_table
JOIN second_table ON second_table.id = first_table.second_table_id;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Complete the following model with a regex validation on :email

A

Class Developer < ActiveRecord::Base
validates :email, format: { with: /\A.@..com\z/ }
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you retrieve a specific record of a given ActiveRecord model?

A

By calling .find(id) on your model
or
By calling .find_by(attribute: value) if you don’t know the id

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Consider the model Doctor, assuming you already created a DB with doctors tables, how would you add a new doctor in your DB?

A

dr_house = Doctor.new(name: “greg”)

dr_house.save

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

You start a new project, what is the next step following the user’s stories writing and mockups drawing, and before writing any line of code?

A
Project steps:
> user's stories
> mockups
> db schema
>> Start coding
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the SQL keyword to retrieve from the DB?

A

SELECT

17
Q

Give the generic syntax of a migration to add a column to a given table?

A
class AddColmnToTable < ActiveRecord::Migration
 def change 
     add_column :table, :column, :type
 end
end