Good effort on this, you are thisclose! Here are my notes on your code:
-
When I go into the console and enter
User.new
for the first time, a newUser
is instantiated, but I also get this warning message:/Users/Phil/.../active-record-associations-drill-shirts-challenge/app/models/user.rb:10: warning: duplicated key at line 10 ignored: :through
/Users/Phil/.../active-record-associations-drill-shirts-challenge/app/models/user.rb:12: warning: duplicated key at line 12 ignored: :through
This is because you are chaining
through:
statements on lines 10 and 12 of yourUser
model. That is a no-no. And unnecessary. Your:through
only needs to get to a table that already has an association to the table you are trying to reach. That's why you don't need two:through
statements. And heads up: per the above warning, Active Record is completely ignoring the second:through
on each line. It is like the second:through
doesn't exist. Test what happens when you delete only the first:through
and then what happens when you delete only the second:through
. Both ways should work. I believe the best practice is to associate with the table closest to the table you are ultimately trying to reach. (Thus, you would delete the first:through
on each of these lines.) -
You don't need to specify the
foreign_key
on this line because Active Record is automatically going to look for apurchaser_id
field since you are associating it topurchaser
. -
You do need to specify the
foreign_key
on this line because AR will be looking for asales_id
field, which does not exist. (You would need to specify:foreign_key => :shirt_id
.)
Keep at it! Any questions, let me know.
-Phil