Misc Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are the 7 routes created for CRUD operations on a resource and their respective action methods?

A

For example, for a resource called photos:

HTTP Verb, Path, Action

GET, /photos, index
GET, /photos/new, new
POST, /photos, create
GET, /photos/:id, show
GET, /photos/:id/edit, edit
PATCH/PUT, /photos/:id, update
DELETE, /photos/:id, destroy

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

What is the difference between POST, PUT and PATCH?

A

REST denotes that:

A request using the POST method should act upon the resource collection; adding a new resource to the collection Example URL: http://example.com/resources

A request using the PUT HTTP verb should act upon a single resource within the collection; replacing the resource wholly upon the server Example URL: http://example.com/resource/1

A request using the PATCH HTTP verb should act upon a single resource within the collection; updating certain attributes upon the resource where it stands Example URL: http://example.com/resource/1 (PATCH is used for partial updates.)

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

How can you define a route to be used with multiple HTTP methods?

A

With match and via: list of http verbs

Eg.

match ‘products/:id’ => ‘products#show’, via: [:get, :post]

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

What route helpers are created for the following route?

get ‘help’ => ‘help#index’, as: ‘help’

A

help_path (“/help”) and help_uri (“http://www.example.com/help”)

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

What are the 7 named paths created for CRUD operations on a resource?

A

photos_pathreturns/photos

new_photo_pathreturns/photos/new

edit_photo_path(:id)returns/photos/:id/edit(for instance,edit_photo_path(10)returns/photos/10/edit)

photo_path(:id)returns/photos/:id(for instance,photo_path(10)returns/photos/10)

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

In what order are parent and child classes action callbacks called?

A

-

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

How do you skip an action callback?

A

Eg

Before action :
skip_before_action :action_name

Around action
skip_action_callback :action_name

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

How can you prevent sql injection attacks?

A

Avoid string concatenation to create your query, and use question marks to pass parameters which will sanitize your query.

For example:

@persons = People.where(“persons.name LIKE concat(‘%’, ?, ‘%’)”, params[:search])

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

How do you specify a read-only attribute?

A

Using attr_readonly method on your model.

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

What is the difference between calling delete or destroy on an object?

A

Destroy loads the instance of the ActiveRecord object and triggers before_destroy callbacks or deletes dependent associations child objects. Delete does not, which means it’s also faster.

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

What are 3 ways in which you can write a query to find users by their city and age?

A

-

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

How do optimistic and pessimistic locking behave in rails?

A

Optimistic locking doesn’t operate on the database level (doesn’t actually lock the tables or rows in the database) but if two users edit the same data, when the second tries to save it throws a StaleObjectError (even when save () is used which doesn’t throw an exception on validation errors.

Pessimistic locking operates at the database level and locks the rows until a first transaction is finished, before it allows other users to read the data.

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

What is the default ordering if no ‘order by’ clause is specified in a query?

A

None actually. This seems to trip people since the common belief is that ‘order by id asc’ is the default

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

How do you get a random record?

A

An example could be using a random offset

Eg.

User.offset(rand(User.count)).limit(1)

It’s important to make sure you don’t load all the data from the table for one row

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

What ruby gems do you like/have you used??

A

Some examples:

Httparty

Aws sdk rails

Activerecord-import - bulk import

Rubocop, byebug

State_machine & aasm

Rspec, fabricator / factory girl

Devise

Resque, Sidekiq

Paperclip, carrierwave

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

What are some disadvantages for using Rails?

A

Fat models; concerns can take some of the load off.

17
Q

What backend queuing systems do you know/have you used?

A

Some examples: DelayedJob, Resque, Sidekiq, AWS SQS (although with this last one the messages can be processed more than once so it shouldn’t be used for email sending)

18
Q

What callbacks do you know?

A

Some examples: before_validation, after_validation, before_create, after_create, before_save, around_save, (which uses yield).

19
Q

What are middlewares used for?

A

Dispatching requests, session handling, parsing params, whitelisting domains

20
Q

What are strong parameters?

A

Rails provides an interface to specify whitelisted attributes and doesn’t allow mass assignment of parameters from action controllers.

For example, how o specify whitelisted attributes:

params.require(:person).permit(:name, :age)

21
Q

What are keyword arguments?

A

Arguments specified by symbols.

def obvious_total(subtotal:, tax:, discount:)
subtotal + tax - discount
end

obvious_total(subtotal: 100, tax: 10, discount: 5) # => 105

  • order of parameters doesn’t matter
  • method call is more readable

vs

def mysterious_total(subtotal, tax, discount)
subtotal + tax - discount
end

mysterious_total(100, 10, 5) # => 105

22
Q

How do you solve N+1 query problems?

A

includes delegates the job to #preload or #eager_load depending on the presence or absence of condition related to one of the preloaded table.

Using eager loading. There are 3 methods that can be uased to achieve this: #includes, #preload or #eager_load.

23
Q

What is the difference between the following two?

scope :from_the_past, where(“happens_at <= ?”, Time.now)

scope :from_the_past, -> { where(“happens_at <= ?”, Time.now) }

A

In the first scenario, Time.now will always be the time when the class was loaded. Using lambdas (second example), the code is lazy loaded, which means it evaluates when called.

24
Q

Rails supports put and patch. Major browsers don’t support these http methods. How does rails deal with this?

A

It actually does a POST under the hood. Forms include a hidden field keeping track of this.

25
Q

Routes: What is the difference between namespaces and scope?

A

Namespace will look the the class in the namespace; scope will not, it will only modify the route.

Eg.
namespace :admin do
resources :users
end

admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
….

routes.rb
scope :admin do
resources :users
end

users GET /admin/users(.:format) users#index
POST /admin/users(.:format) users#create
…..

26
Q

What is the difference between symbols and strings? When would you use one over the other?

A

Symbols are immutable and reusable, retaining the same object_id.

Symbols can cause memory leaks when used incorrectly in versions before 2.2 - once they were instantiated, that memory was never free again until an app restart. So before 2.2, you wouldn’t want to instantiate symbols for user provided data. Ruby 2.2 introduced symbol garbage collector though.

https://www.infoq.com/news/2014/12/ruby-2.2.0-released:

The introduction of GC for symbols, a kind of string identifier, also improves Ruby memory management. So much so that Ruby on Rails 5.0, targeted for Fall 2015, will only target Ruby 2.2+ due to this change:

Rails 5.0 will target Ruby 2.2+ exclusively. There are a bunch of optimizations coming in Ruby 2.2 that are going to be very nice, but most importantly for Rails, symbols are going to be garbage collected. This means we can shed a lot of weight related to juggling strings when we accept input from the outside world. It also means that we can convert fully to keyword arguments and all the other good stuff from the latest Ruby.

27
Q

What is the difference between kind of, instance of, is a

A

-

28
Q

What is a code smell that you have hidden duck types in your code?

A

-

29
Q

What are some examples of inheritance /automatic delegation?

A

Inheritance, modules, duck typing