1. What is Ruby on Rails?
BeginnerAnswer: Web application framework following MVC pattern
Ruby on Rails is a server-side web application framework written in Ruby. Follows MVC (Model-View-Controller) pattern. Emphasizes convention over configuration and DRY principles.
2. What is service object pattern?
AdvancedAnswer: Extracts business logic into dedicated classes
Service objects encapsulate business logic. Keep controllers, models thin. app/services/. Single responsibility. call method convention. Better organization, testability.
3. What are associations?
IntermediateAnswer: Relationships between models: has_many, belongs_to, has_one
Associations define model relationships. has_many, belongs_to, has_one, has_and_belongs_to_many, has_many :through. Provide convenient methods. Define in model classes.
4. What is belongs_to?
IntermediateAnswer: Defines one-to-one relationship from child side
belongs_to defines child side: class Post < ApplicationRecord; belongs_to :user; end. Expects user_id foreign key. Provides user method. Singular.
5. What does MVC stand for?
BeginnerAnswer: Model-View-Controller
MVC: Model (data/business logic), View (presentation), Controller (handles requests, coordinates). Separation of concerns. Core architectural pattern in Rails.
6. What is form object pattern?
AdvancedAnswer: Handles complex forms with multiple models
Form objects handle complex forms. Coordinate multiple models. Include ActiveModel::Model. Validations, save logic. Keep controllers clean. Better than accepts_nested_attributes_for.
7. What is query object pattern?
AdvancedAnswer: Encapsulates complex queries
Query objects extract complex queries. Keep models clean. Reusable, testable. app/queries/. Return ActiveRecord::Relation for chaining. Single responsibility.
8. What is has_many?
IntermediateAnswer: Defines one-to-many relationship from parent side
has_many defines parent side: class User < ApplicationRecord; has_many :posts; end. Provides posts collection. Plural. Can add dependent options.
9. How do you create a new Rails application?
BeginnerAnswer: rails new app_name
Create app with rails new app_name. Options: --database=postgresql, --skip-test. Creates complete directory structure. Run bundle install automatically.
10. What is has_many :through?
IntermediateAnswer: Many-to-many through join model
has_many :through creates many-to-many via join model. has_many :comments, through: :posts. More flexible than HABTM. Can add attributes to join.
11. What is decorator pattern?
AdvancedAnswer: Adds presentation logic without modifying model
Decorators add presentation logic. Use Draper gem. Keep models clean. Delegate to model. app/decorators/. Alternative to bloated helpers.
12. What is the Rails directory structure?
BeginnerAnswer: app/, config/, db/, public/, test/, lib/, etc.
Main directories: app/ (MVC code), config/ (configuration), db/ (database), public/ (static files), test/ or spec/ (tests). Convention over configuration.
13. What is API mode?
AdvancedAnswer: Rails configured for API-only applications
API mode: rails new app --api. No views, assets. Lighter middleware. ApplicationController < ActionController::API. JSON responses. Use for backend APIs.
14. What are validations?
IntermediateAnswer: Ensure data integrity before saving to database
Validations ensure data integrity: validates :name, presence: true. Run before save. Prevent invalid data. Add errors to model. Display in views.
15. What is a model?
BeginnerAnswer: Represents data and business logic, inherits from ApplicationRecord
Model represents data/business logic. Inherits from ApplicationRecord (Active Record). Maps to database table. Handles validations, associations, queries.
16. What are common validations?
IntermediateAnswer: presence, uniqueness, length, format, numericality
Common: presence (required), uniqueness, length, format (regex), numericality, inclusion, exclusion. Custom validators possible. Multiple per field.
17. What is serialization?
AdvancedAnswer: Converting models to JSON/XML
Serialization converts to JSON/XML. Use ActiveModel::Serializers, fast_jsonapi, jbuilder. Control output shape. API responses. Versioning consideration.
18. What is a controller?
BeginnerAnswer: Handles requests, coordinates models and views
Controller receives requests, interacts with models, renders views. Inherits from ApplicationController. Actions are public methods. RESTful by convention.
19. What is GraphQL?
AdvancedAnswer: Query language for APIs, alternative to REST
GraphQL provides flexible API queries. Use graphql-ruby gem. Single endpoint. Clients request exactly needed data. Alternative to REST. More complex but powerful.
20. What are callbacks?
IntermediateAnswer: Methods triggered at lifecycle events
Callbacks run at specific points: before_save, after_create, before_validation. Define in models. Use for side effects, computed fields. Can skip with save(validate: false).
21. What is a view?
BeginnerAnswer: Presents data, typically HTML templates with embedded Ruby
Views render presentation. Typically ERB templates (.html.erb). Can access instance variables from controller. Layouts wrap views. Partials for reusable components.
22. How do you generate a model?
BeginnerAnswer: rails generate model Name field:type
Generate model: rails generate model User name:string email:string. Creates model file, migration, test. Can use shorthand: rails g model. Specify field types.
23. What is database sharding?
AdvancedAnswer: Horizontal partitioning across databases
Sharding splits data across databases. Horizontal scaling. Rails 6+ multi-database support. Connect to multiple databases. Complex but enables scaling.
24. What are scopes?
IntermediateAnswer: Named queries for reuse
Scopes are reusable queries: scope :published, -> { where(published: true) }. Chain scopes. Keep queries DRY. Use lambdas for arguments. Class methods alternative.