Get in Flow and Go
Day 62 of Flatiron School
It felt so good to spend almost the entire day coding. I missed it. My progress was VERY SLOW at times (or so it felt). Things like the :source
option in Active Record were foreign and not really taught but needed to solve a large lab I was working on starting first thing this morning. Luckily I have access to some awesome help. Here’s one conversation I had today:
Seth Alexander 34 MINUTES AGO
Need some help understanding thehas_many
option:source
PJ 34 MINUTES AGO
Hey there Seth how’s it gong?Seth Alexander 34 MINUTES AGO
Pretty good.Seth Alexander 33 MINUTES AGO
So in theUser
model for this lab I get a test passing using:Seth Alexander 33 MINUTES AGO
has_many :trip_listings, :through => :trips, :source => :listing has_many :hosts, :through => :trip_listings, :foreign_key => 'host_id'
Seth Alexander 32 MINUTES AGO
I’ve read the Rails Guide and Googled but still don’t grasp what is actually happening when I use:source
Seth Alexander 32 MINUTES AGO
If you could walk me through step by step what my code is doing, that’s what I’m looking for.PJ 24 MINUTES AGO
Definitely it’s a pretty new topic give me one sec though sorry about hte wait!Seth Alexander 23 MINUTES AGO
No problem, I’m not actually stuck, just trying to understand better.PJ 19 MINUTES AGO
no worries! so the idea behind source is pretty fundamental to polymorphic associations - having one object that can be used as essentially a different object with the same propertiesPJ 17 MINUTES AGO
an easy example to think about is a family tree - instead of making a new object for every single member of the family you could have one Person classPJ 15 MINUTES AGO
and then have different associations using the same source objectPJ 13 MINUTES AGO
in this situation you have a User that has listings but since they’re not their own listings (think property manager) but as a guest they namespace them as atrip_listing
Seth Alexander 10 MINUTES AGO
So are we basically “reaching” intotrips
and pulling out thelisting
method then setting it totrip_listings
for the User model?PJ 9 MINUTES AGO
well listing is an object so not as much reach for it’s method as reaching for it’s relationship to the objectPJ 9 MINUTES AGO
which sinceuser has many trips
it also has whatever relationships come with itPJ 8 MINUTES AGO
which is sort of the argument against usingOO programming
because when you have an object with relationships all of a sudden you have everything that it hasPJ 7 MINUTES AGO
there’s a saying that goes “I wanted a banana and you gave me a gorilla in the jungle holding a banana”Seth Alexander 7 MINUTES AGO
Sotrip_listings
is setting up an association between User and listing > objects for the trips it has many of.PJ 6 MINUTES AGO
between User as a guestPJ 6 MINUTES AGO
but yes that’s correct!Seth Alexander 4 MINUTES AGO
Okay, I guess I was thinking too much about the methods that got enabled and not the whole scope of things. Like,User.trip_listings == User.trips.listings
and not the broader picture.PJ 2 MINUTES AGO
no worries!
Besides the lab that took me about 6 hours I moved along through the material. I had another block where some help was needed and it happened to be that I had one work mixed up. Took about 30 mins to find but once it was found the entire lab worked. It was awesome. The quantity of lessons & labs I’m completely daily is definitely going down lately but the bites are getting larger too. I hope to pick the pace back up (hours in code that is) and get back to where I was when I started. I think I’ll feel better about my progress if I’m at that pace again. It also feels good to get stuff done and see more progress.
Time spent today: 9:40
Time spent total: 232:07
Lessons completed today: 7
Lessons completed total: 457
Share this post
Twitter
Facebook
LinkedIn
Email