Ruby on Rails Test 1 — Questions and Answers
Question 1: The representation of a resource is referred to as...
- Action pack
- Ruby make
- Camel case
- Web page (Correct answer)
Correct answer: Web page
In the context of web development and RESTful principles often followed by frameworks like Ruby on Rails, a 'resource' (such as a user or a product) is typically represented to the user through a web page. This web page displays the resource's data and often provides interfaces for interacting with it, such as editing or deleting. It serves as the visual and interactive representation of the underlying data.
Question 2: What is the distinction between model and controller names?
- the data being presented by the web page
- model names are singular, controller (and table) names are plural (Correct answer)
- the method within the controller to call
- none of the above
Correct answer: model names are singular, controller (and table) names are plural
Ruby on Rails adheres to strict naming conventions as part of its 'convention over configuration' philosophy. Model names are conventionally singular (e.g., `User`), representing a single instance of a resource. In contrast, controller names and their corresponding database table names are plural (e.g., `UsersController`, `users` table), as they typically handle collections of those resources.
Question 3: What design pattern is frequently used in rail models?
- Layout
- ActiveRecords (Correct answer)
- Route
- Controller
Correct answer: ActiveRecords
Ruby on Rails heavily utilizes the Active Record design pattern for its models. Active Record provides an object-relational mapping (ORM) system that maps database tables to Ruby classes and rows to objects. This allows developers to interact with the database using Ruby objects and methods, greatly simplifying data persistence and retrieval within the application.
Question 4: Which of these is not a Rails feature?
- Models (Correct answer)
- Convention over Configuration
- Active Record
- Scaffolding
Correct answer: Models
While Ruby on Rails applications are built on the Model-View-Controller (MVC) architectural pattern, 'Models' as a generic concept for representing data and business logic are not a feature exclusive to Rails. Rails specifically implements its model layer using the Active Record pattern, which is a distinct feature of the framework. Therefore, if the question implies a unique or specific feature of Rails, 'Models' in a general sense might be considered less specific than Active Record, Convention over Configuration, or Scaffolding.
Question 5: What is the file naming convention?
- Validate
- Web page
- ActiveRecords
- Underscores (Correct answer)
Correct answer: Underscores
Ruby on Rails follows specific naming conventions to maintain consistency and leverage 'convention over configuration.' For file names, especially for classes, modules, and views, the convention is to use `snake_case`, where words are separated by underscores. For example, a `UserProfile` model would be defined in `user_profile.rb`, and a `UsersController` would be in `users_controller.rb`.
Question 6: What exactly is the scaffold command?
- Ruby Version Manager. Install and manage multiple versions of Ruby.
- rails generate scaffold User name:string email:string (Correct answer)
- self-contained bundles of gems. Useful for versioning
- self-contained packages of Ruby code
Correct answer: rails generate scaffold User name:string email:string
The `rails generate scaffold` command is a powerful tool in Ruby on Rails for quickly generating the basic structure (scaffolding) for a new resource. It automatically creates a model, controller, views, routes, and migrations for a given resource, including specified attributes (like `name:string` and `email:string`). This allows developers to rapidly prototype or build out CRUD (Create, Read, Update, Delete) functionality.
Question 7: How does rake determine which migrations have been completed and which have not?
- because they make the partial less dependent on other code
- rails generate migration Add Phone Tickets phone:string
- it can do more complex things like modifying the database structure and running tests
- rails records the latest timestamp of all the migrations it runs (and runs only the latest ones) (Correct answer)
Correct answer: rails records the latest timestamp of all the migrations it runs (and runs only the latest ones)
Rails migrations are managed by tracking their execution status in the database to ensure they are applied only once. When a migration is run, Rails records its unique timestamp (which is part of the migration file name) in a special `schema_migrations` table. When `rake db:migrate` is executed, Rails checks this table to identify which migrations have already been applied and only runs the pending migrations with timestamps later than those recorded.
Question 8: Is it true that when you put something into an array, the array keeps a duplicate of it?
- the data being presented by the web page
- no. arrays just keep references to objects stored in memory (Correct answer)
- the request parameters table
- it is not associated with a model
Correct answer: no. arrays just keep references to objects stored in memory
In Ruby, similar to many object-oriented languages, arrays store references (or pointers) to objects, not copies of the objects themselves. When an object is added to an array, the array holds a reference to that object's location in memory. This means that if the object is modified through one reference, those changes will be visible through any other reference to the same object, including those stored in arrays.
Question 9: What is the name of the online application that merges data and services from various sources on the internet?
- mashup (Correct answer)
- a fixture
- an action
- form_for
Correct answer: mashup
A 'mashup' is a web application that combines data, presentation, or functionality from two or more external sources to create a new, integrated service. This often involves using APIs to pull information from different websites or services and then presenting it together in a novel way. A common example is combining mapping data with real estate listings.
The representation of a resource is referred to as...