Ruby on Rails Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the difference between the new and build method in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a concern in Rails?

A

In Rails, a concern is a module that encapsulates and shares reusable functionality across different models or controllers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a helper in Rails?

A

In Rails, a helper is a module that encapsulates and shares reusable logic and formatting across multiple views.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we call the initialize method of the parent class, ensuring any setup logic defined there runs in the child class as well?

A

By calling super() in the child class initialize method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do we call the method that allows access to an instance variable in a class?

A

An accessor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the three types of accessors we have in Rails?

A

attr_reader: Only a getter (read-only)
attr_writer: Only a setter (write-only)
attr_accessor: Both getter and setter (read-write)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a symbol in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the advantages of using symbols in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly