Rails Flashcards
Explain belongs to association
The belongs_to association is used to specify that one model “belongs to” another model. It establishes a one-to-one or many-to-one relationship where the model with the belongs_to association is associated with a single instance of another model.
Example:
ruby
Copy code
class Author < ApplicationRecord
belongs_to :book
end
class Book < ApplicationRecord
has_many :authors
end
book = Book.first
author = book.authors.build(name: “John Smith”)
Explain has one association.
The has_one association is used to specify that one model has a relationship with another model, where the first model “has one” instance of the second model. It establishes a one-to-one relationship.
Example:
ruby
Copy code
class User < ApplicationRecord
has_one :profile
end
class Profile < ApplicationRecord
belongs_to :user
end
user = User.first
profile = user.build_profile(username: “johnsmith”)
Explain has many associations.
The has_many association is used to specify that one model can have multiple instances of another model. It establishes a one-to-many relationship.
Example:
ruby
Copy code
class Author < ApplicationRecord
has_many :books
end
class Book < ApplicationRecord
belongs_to :author
end
author = Author.first
book = author.books.create(title: “Ruby 101”)
Explain has_and_belongs_to_many
The has_and_belongs_to_many association, also known as HABTM, is used to specify a many-to-many relationship between two models. It does not require an intermediary join model and uses a join table to establish the relationship.
Example:
ruby
Copy code
class Product < ApplicationRecord
has_and_belongs_to_many :categories
end
class Category < ApplicationRecord
has_and_belongs_to_many :products
end
product = Product.first
category = Category.first
product.categories «_space;category
Explain polymorphic associations
Polymorphic Associations:
Polymorphic associations allow a model to belong to more than one other model, on a single association. This is useful when a model can be associated with multiple other models, but the specific type of association can vary.
Example:
ruby
Copy code
class Image < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class User < ApplicationRecord
has_many :images, as: :imageable
end
class Product < ApplicationRecord
has_many :images, as: :imageable
end
user = User.first
image = user.images.create(url: “example.com/image.jpg”)
product = Product.first
image = product.images.create(url: “example.com/image.jpg”)
Explain self joins.
Self Joins:
Self joins occur when a model has a relationship with other instances of the same model. It allows a model to have a one-to-many or many-to-many relationship with itself.
Example:
ruby
Copy code
class Employee < ApplicationRecord
belongs_to :manager, class_name: “Employee”, optional: true
has_many :subordinates, class_name: “Employee”, foreign_key: “manager_id”
end
employee = Employee.create(name: “John Smith”)
subordinate1 = employee.subordinates.create(name: “Jane Doe”)
subordinate2 = employee.subordinates.create(name: “Bob Johnson”)
manager = subordinate1.manager
what does MVC stand for?
MVC stands for Model-View-Controller. It is a software architectural pattern commonly used in the development of web applications. Here’s a brief explanation of each component:
Model: The model represents the data and business logic of the application. It encapsulates the application’s data and provides methods to manipulate and access that data. It is responsible for maintaining consistency and integrity of the data.
View: The view is responsible for the presentation layer of the application. It defines how the data from the model should be displayed to the user. Views are usually comprised of HTML, CSS, and other presentation technologies. They receive data from the model and present it in a user-friendly format.
Controller: The controller acts as an intermediary between the model and the view. It handles user input and decides how to update the model or the view accordingly. The controller receives input from the user through the view, processes it, and interacts with the model to perform necessary operations or retrieve data. It then updates the view with the updated data from the model.
What’s a callback in the context of RoR?
Ruby on Rails Active Record Callbacks:
In Ruby on Rails, Active Record provides various callbacks that get triggered at different points in an object’s life cycle. For example:
before_save and after_save: These callbacks are triggered before and after an object is saved to the database.
before_create and after_create: These callbacks are triggered before and after an object is created and saved to the database for the first time.
before_update and after_update: These callbacks are triggered before and after an object is updated and saved to the database.
Explain includes.
The includes method is used to eager load associations in ActiveRecord. It allows you to fetch the main records along with their associated records in a more efficient manner. When using includes, ActiveRecord performs a separate query to load the associated records, reducing the number of database queries executed.
Example:
ruby
Copy code
users = User.includes(:posts).all
users.each do |user|
puts user.posts
end
In this example, the includes(:posts) method ensures that the posts associated with each user are loaded in a separate query, improving performance by avoiding the N+1 query problem.
Explain preload.
The preload method is another way to eager load associations in ActiveRecord. It fetches the associated records in separate queries, similar to includes, but it does not perform any eager loading of the associations.
Example:
ruby
Copy code
users = User.preload(:posts).all
users.each do |user|
puts user.posts
end
In this example, preload(:posts) ensures that the associated posts are preloaded with separate queries. However, unlike includes, it does not automatically join the tables or eager load the associations. This means that if you access the association, a separate query will be executed for each record.
Explain eager load.
The eager_load method is yet another way to eager load associations in ActiveRecord. It fetches the main records and their associated records in a single query by performing a SQL join operation. It fully loads the associations upfront, eliminating the need for additional queries when accessing the associated records.
Example:
ruby
Copy code
users = User.eager_load(:posts).all
users.each do |user|
puts user.posts
end
In this example, eager_load(:posts) performs a SQL join to fetch the associated posts along with the users. This results in a single query that loads all the necessary data, making it an efficient way to eagerly load associations.