Closed kylecrsn closed 6 years ago
It looks like you renamed rental's owner_id
to user_id
. Why? (It breaks my PR)
Very nice work BTW! It's a really slick site.
I think we'll have to work together to merge our mutual work on the seed file structure.
I can explain it more during lec/lab, but rails has a feature called counter_cache which makes getting stats about the count of a model's things practical (ie if you're twitter and you want to display that a user has 30,000 tweets on their profile page, making an sql query like 'Tweets.find(user_id: session[user_id]).count' is impractical). Counter cache does this by
In our existing application, we had that a rental didn't belong to a user (owner), but rather has_one user, through owner id. Using has_one instead of belongs_to made counter cache not usable, so I changed the relationship to allow it.
I tried alternatives before this like aliasing(https://stackoverflow.com/questions/21471075/possible-to-alias-a-belongs-to-association-in-rails, see below) but couldn't get any of them to work.
belongs_to :user
belongs_to :owner, :class_name => :User, :foreign_key => "user_id"