What does the collect method of the Array class do?
Ans: It takes all the items returned by the code block and collects them
together into a single array. For example:
a = {"dog", "cat", "mouse"]
b = a.collect do |x|
x.length
end
print b, "\n"
The output is s[3, 3, 5], which is the lengths of the strings
in the array.
What is a one-to-many relationship?
Ans: A correspondence between two database tables where a row in
the primary table is matched with zero or more rows in the secondary
table.
What is a primary key? What is a foreign key?
Ans: In a Rails table, the primary key is the id, which is the row number of
the table. This row number uniquely identifies the table row.
A foreign key is a field in a database table that contains the primary
key of a different table.
How do you set up a one-to-many relationship in Rails?
Create a scaffold or model for the primary and secondary models.
Put statements like this in the primary and secondary model files:
has_many :awards
belongs_to :student
Can you think of an example involving three models A, B, and C,
where there is a one-to-many relationship between A and B and there
is also a one-to-many relationship between models B and C?
Ans: Let A = Airline, B = Flight, C = Passenger. An airline has many
flights, and a flight has many passengers.
Create a Rails scaffold project with two
models Patient and Doctor. Use this seed file
code to get the field names and to populate the database tables.
Then add a new controller named DocInfo with a view display that
shows a table with the Patient names and his or her doctor's name and phone
number,
shows an unordered list with the Doctor names with an unordered list
of Patient name and age for each doctor.