Ruby on Rails Flashcards
What is the difference between the new and build method in Rails?
new is a built-in Rails method for initializing a new object in memory without saving it to the database :
@school_class = SchoolClass.new
build is a shortcut to associate the new object with a parent record without saving it to the database:
@school_class = @program.school_classes.build
in terms of functionalities:
@program.school_classes.build & @program.school_classes.new are equivalent even though the first one is recommended.
@school_class = SchoolClass.build is not valid.
What is a concern in Rails?
In Rails, a concern is a module that encapsulates and shares reusable functionality across different models or controllers.
What is a helper in Rails?
In Rails, a helper is a module that encapsulates and shares reusable logic and formatting across multiple views.
How do we call the initialize method of the parent class, ensuring any setup logic defined there runs in the child class as well?
By calling super() in the child class initialize method.
How do we call the method that allows access to an instance variable in a class?
An accessor
What are the three types of accessors we have in Rails?
attr_reader: Only a getter (read-only)
attr_writer: Only a setter (write-only)
attr_accessor: Both getter and setter (read-write)
What is a symbol in Rails?
In Rails, a symbol is a lightweight, immutable identifier that starts with a colon (:) and is used as a constant label for values that don’t need to change.
What are the advantages of using symbols in Rails?
Better readability: Symbols like :show, :admin, and :index serve as clear labels for developers.
Performance optimization: Symbols are stored once in memory and reused, which can reduce memory usage in large applications.
For example, :user and :user refer to the same object in memory, whereas two identical strings like “user” and “user” are separate objects.