You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imagine we want to display a message like "Hello, John" in the header of each page. In our controller we would say Customer.find(17) and use @customer.first_name in our view. Rails will run a SQL statement like this:
SELECT * from CUSTOMERS where ID='17';
And, of course, that’s going to fetch the entire row from our customers table. We’re trying to get the first_name, so why bother fetching the birthday, gender, and home_city every request? Instead, we could create a one-to-one relationship between a customer and their "details".
The argument made assumes that you can only get ALL columns via SQL.
This is not true, as you can obviously select only particular columns.
SELECT first_name, birthday FROM CUSTOMERS where ID='17';
Stronger justification and reasons could be given.
I think a mention of something like the following might be helpful:
In a regular SQL query you can select just a particular column with SELECT first_name but because active record must always build a complete object, active record must always issue it's queries with SELECT *.
There might be some more verbiage that helps explain the scenario.
I'm just considering the perspective of developers like myself who are not new to SQL, but new to Rails/ActiveRecord
The text was updated successfully, but these errors were encountered:
https://github.com/turingschool/curriculum/blob/master/source/topics/models/relationships.markdown#problem-scenario
The argument made assumes that you can only get ALL columns via SQL.
This is not true, as you can obviously select only particular columns.
Stronger justification and reasons could be given.
I think a mention of something like the following might be helpful:
There might be some more verbiage that helps explain the scenario.
I'm just considering the perspective of developers like myself who are not new to SQL, but new to Rails/ActiveRecord
The text was updated successfully, but these errors were encountered: