rubyintermediate
Ruby on Rails
MVC, routing and ActiveRecord
7 questions
By EZ4Code Team
1. What is the core architectural pattern of Rails?
MVC (Model-View-Controller)
MVVM
MVP
Microservices
Explanation: Rails follows MVC: Model (ActiveRecord) handles data, View (templates) renders, Controller (ActionController) coordinates requests.
2. How are RESTful routes declared in Rails?
resources :posts
route :posts
rest :posts
map :posts
Explanation: resources :posts automatically generates seven RESTful routes: index/show/new/create/edit/update/destroy.
3. What is ActiveRecord in Rails?
The ORM layer; model classes inherit from ActiveRecord::Base to map to database tables
The view layer
The routing layer
The controller layer
Explanation: ActiveRecord is the ORM framework; Models inherit from ActiveRecord::Base, automatically mapping to tables and providing query interfaces (e.g. Post.where(...)).
4. How is Convention over Configuration reflected in Rails?
PostsController corresponds to the posts route, the Post model, and the posts table
All mappings must be configured manually
No database is used
A routing table must be configured
Explanation: Rails automatically associates through naming conventions: PostsController -> post_path routes -> Post model -> posts table, reducing configuration.
5. What is the command to generate a new model in Rails?
rails generate model Post title:string
rails new model Post
rails create Post
rails model Post
Explanation: rails generate model ModelName field:type generates the model class and migration file, e.g. rails g model Post title:string body:text.
6. What is the purpose of Rails migrations?
To manage database schema changes with Ruby code, versioned and rollback-able
To migrate data to a new server
To back up data
To generate model code
Explanation: Migrations use a Ruby DSL to describe schema changes (such as create_table/add_column), executed via rails db:migrate, and can be rolled back.
7. What is the purpose of strong parameters in Rails?
To prevent mass assignment vulnerabilities by explicitly permitting allowed fields
To encrypt parameters
To validate parameter types
To cache parameters
Explanation: Strong parameters require explicitly declaring the fields allowed for mass assignment in the controller via params.require(:model).permit(:field1, :field2), preventing malicious field injection.