Test rails 1 - Sheet1 (1) Flashcards
What is Active Record in Rails?
Active Record is the Object-Relational Mapping (ORM) layer in Rails that allows developers to interact with the database using Ruby objects. It automatically maps tables to classes and rows to instances of those classes. It provides methods for creating, reading, updating, and deleting records without needing SQL. Active Record also supports associations between models, validations, and callbacks.Active Record is the Object-Relational Mapping (ORM) layer in Rails that allows developers to interact with the database using Ruby objects. It automatically maps tables to classes and rows to instances of those classes. It provides methods for creating, reading, updating, and deleting records without needing SQL. Active Record also supports associations between models, validations, and callbacks.
Explain belongs_to in Rails.
belongs_to defines a one-to-one connection between two models, where one model contains the foreign key of another. This association is typically used when a child model references a parent model. For example, if a Post belongs to a User, the posts table will have a user_id column that points to the users table. Rails automatically provides helper methods for accessing the associated user from a post.
What does self refer to in Ruby?
self in Ruby refers to the current object on which a method is being executed. Within an instance method, self points to the instance of the class. Inside a class method, self refers to the class itself. Understanding self is crucial for distinguishing between class methods and instance methods, and for writing methods that modify the current object.