Rails Flashcards
How does the development of any new rails app start?
rails new MY_APP
What does the app folders contain in a Rails project?
app
|–assets
|–controllers
|–models
|–views
How do you build a link to the homepage of a Rails app?
<%= link_to “Home”, root_path %>
How do you build a link to an inner page in a Rails app?
You need to use link_to with the correct path.
you can build the link:
<%= link_to “Contact”, contact_path %>
which results in:
<a>Contact</a>
How does the development of any new rails app start?
rails new MY_APP
How can you append elements of an array or another enumerable object to the end of an existing arrayin Ruby?
.concat
How can you substract elements that in an array, from another array?
array1 -= array2
In this example, array1 -= array2 removes elements from array1 that are also present in array2. So, after the operation, array1 contains only the elements that are unique to array1.
How is .take diffrent from .first in Ruby?
.first is specific for arrays to take the first object(s).
.take is more flexible because it works not only on arrays but also on other enumerable objects like database queries or generators and can also limit how many database entries you take for instance.
How can you sort a hash?
hash.sort_by { |key, value| -value }[0…num].to_h
The sort_by method is called on the counter hash, and it takes a block as an argument.
The -value inside the block negates each value, which effectively sorts the hash in descending order of values.
Since array indexing starts from 0, we need to use … so the correct number of elements is taken.
After selecting the top elements as a range from the sorted array, .to_h is called to convert this range into a hash.
How do you process a csv file?
File.open(filename, “r”).each_line do |line|
codeblock
end
How can you create in Ruby a Key Value pair with automatic incrementing of the value?
hash[key] += 1
What does this method do?
counter = Hash.new(0)
stop_words = load_stop_words(stop_words_filename)
File.open(filename, “r”).each_line do |line|
line.chomp.downcase.split(/\W+/).each do |word|
counter[word] += 1 unless stop_words.include? word
end
end
Initializes a hash counter with each new key as standard value 0.
- Loads a method for stop_words
- Opens a csv file and itrerrates over each line.
- removes the line break from the line, downcases it and splits it at non word characters.
- creates each word as key with the standard value of zero and increments its value unless it is a word that is in the stop_words array.
Get Jules Verne’s books
jules.books
Get author with name=”Jules Verne”, store it in a variable: jules
jules = Author.find_by(name: “Jules Verne”)
Add your favorite author (name)(class Authors) to the DB.
Author.create(name: “Paul Auster”)
Write a migration to add a category column to the books table.
class AddCategoryToBooks < ActiveRecord::Migration[7.0]
def change
add_column :books, :category, :string
end
end
Complete the following migrations to create your e-library database for title, year, foreig id, and another important element.
class CreateBooks < ActiveRecord::Migration[7.0]
def change
end
end
end
class CreateBooks < ActiveRecord::Migration[7.0]
def change
create_table :books do |t|
t.string :title
t.integer :year
t.references :author, foreign_key: true
t.timestamps
end
end
end
What is the SQL query generated by #destroy:
doctor.id
# => 1
doctor.destroy
DELETE FROM doctors WHERE id = 1;
What are the two SQL actions mapping Active Record method #save?
If #save is called on a new instance then it maps to INSERT INTO and UPDATE on a existing record.
ActiveRecord Advanced
What are the most common validation types?
Presence, uniqueness (stand alone or using a scope), length (min or max), format (using Regexp).
Complete the following model with a 120-character minimum length validation on :description:
class Developer < ActiveRecord::Base
# TODO
end
class Developer < ActiveRecord::Base
validates :description, length: { minimum: 120 }
end
What Active Record class method can you call when you want to delete all instances of a given model?
.destroy_all
Write the migration to add an intern_id foreign key in patients table
class AddInternReferenceToPatients < ActiveRecord::Migration[7.0]
def change
add_reference :patients, :intern, foreign_key: true
end
end
Complete the following migration file to add a zipcode column in the cities table:
class ??? < ActiveRecord::Migration[7.0]
def change
# TODO
end
end
class AddZipcodeToCities < ActiveRecord::Migration[7.0]
def change
add_column :cities, :zipcode, :string
end
end