Week 1 Questions Flashcards
what does update_attributes do?
The update_attributes method accepts a hash of attributes, and on success performs both the update and the save in one step (returning true to indicate that the save went through). Note that if any of the validations fail, such as when a password is required to save a record, the call to update_attributes will fail. If we need to update only a single attribute, using the singular update_attribute bypasses this restriction:
» user.update_attribute(:name, “El Duderino”)
=> true
» user.name
=> “El Duderino”
how would you find a user or any other model by specific attributes like and email or username?
User.find_by(email: “mhartl@example.com”)
how do you develop in the rails console without making any changes to the application?
rails console –sandbox
what do migrations do?
Migrations provide a way to alter the structure of the database incrementally, so that our data model can adapt to changing requirements.
how do you write a named route?
#original route name: get 'static_pages/help'
#named route name get '/help', to: 'static_pages#help'
what are the 3 types of assets and how are they used?
- app/assets: assets specific to the present application
- lib/assets: assets for libraries written by your dev team
- vendor/assets: assets from third-party vendors
why use instance variables?
they are automatically available in the views and they can be used throughout a class
what is a difference between single quotes and double quotes?
There’s an important difference, though; Ruby won’t interpolate into single-quoted strings:
» ‘#{foo} bar’ # Single-quoted strings don’t allow interpolation
=> “#{foo} bar”
how do you save changes on a branch and then merge them with the master?
$ git add -A $ git commit -m "Finish static pages" $ git checkout master $ git merge static-pages $ git push
explain this ruby idiom: a||=b
a =b when a == false
otherwise a remains unchanged
What is a Module? Can you tell me the difference between classes and modules?
Modules are about providing methods that you can use across multiple classes - think about them as “libraries” (as you would see in a Rails app). Classes are about objects; modules are about functions.
For example, authentication and authorization systems are good examples of modules. Authentication systems work across multiple app-level classes (users are authenticated, sessions manage authentication, lots of other classes will act differently based on the auth state), so authentication systems act as shared APIs.
You might also use a module when you have shared methods across multiple apps (again, the library model is good here).
modules serve as mechanisms for namespaces
modules provide as a mechanism for multiple inheritance via mixins and cannot be instantiated like classes can
String vs Symbol
Symbols are immutable: Their value remains constant.
Multiple uses of the same symbol have the same object ID and are the same object compared to string which will be a different object with unique object ID, every time.
What is a callback?
Callbacks are methods that get called at certain moments of an object’s life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
Should a callback be public?
It is considered good practice to declare callback methods as protected or private. If left public, they can be called from outside of the model and violate the principle of object encapsulation.
What is the difference between private and protected?
- protected methods can be called by any instance of the defining class or its subclasses.
- private methods can be called only from within the calling object. You cannot access another instance’s private methods directly.
How do you display all errors for a user model?
user.errors.full_messages
What should be the maximum length for an email attribute and why?
Maximum length of strings for many databases is 255