The Pieces Are Coming Together

Feeling great about today. The things that a struggled with the most I ended up cementing (it’s not dry yet) into my brain. The Learn community is awesome I must say. After 2 hours with a Learn Expert (basically a TA), we were both sufficiently stumped. I dropped a note into the student’s Slack channel and <20 minutes it was figured out by the group. So you’re probably wondering what it was…

<h1><%= @post.title %></h1>
<p>
  <%= @post.content %>
</p>
<% @post.categories.each do |c| %>
  <h3><%= c.name %></h3>
<% end %>
<% @post.comments.each do |c| %>
  <p><%= c.user.username %> says: <%= c.content %></p>
<% end %>
<% @post.users.uniq.each do |u| %>
  <%= link_to u.username, user_path(u) %>
<% end %>
<%= form_for @comment do |f| %>
  <%= f.label :content %><br />
  <%= f.text_area :content %><br />
  <%= f.collection_select :user_id, User.all, :id, :username, include_blank: true %><br />
  <%= fields_for :comment_user_attributes, User.new do |u_fields| %>
    <%= u_fields.text_field :username %>
  <% end %><br />
  <%= hidden_field_tag 'comment[post_id]', @post.id %>
<%= f.submit %>
<% end %>

Yeah, it’s in that code… can you find it??? Well, I wrote it and I couldn’t find it so if you can’t all good. If you’re still trying don’t scroll down lol. Here’s the fix with just the line of code that had the error:

<%= f.fields_for :user_attributes, User.new do |u_fields| %>

Yeah, I was missing the f. before fields_for which makes a BIG difference. I can confidently say that I’ll most likely never make that mistake again. I also started taking screenshots during lecture videos of things I wanted to remember and write down. Figured this would be more effective than writing down the time in the video things happen. Although, I did write down a couple larger concepts if I ever need to go back and look.

Something I was struggling with early was collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) and reading the APIdock didn’t help. To the Slack channel for people doing Rails I went. Got some solid info. Then later in a lecture video, Avi goes through an almost identical line of code and what it means. Here’s what I got (saved as a screenshot that I’ll write out by hand in my notebook later):

For context higher up in the code: <%= form_for @post do |f| %>
<%= f.collection_check_boxes(:category_ids, Category.all, :id, :name) %>
Will generate a checkbox named post[category_ids][] for each category in Category and it will use the :id as the value for the checkbox and it will use the :name method as the label for the checkbox.

Exactly what I was looking for was right around the corner when I finished the lab. This isn’t the first time I’ve experienced this. There have been many a lecture where after I struggled through something it all became clearer. Specific topics that I had questions about were explained in a way that answered my questions.

Side note, I’ve realized I need something to rest my wrists on. Especially my right hand that uses the mouse. This Microsoft Natural Keyboard has a nice soft wrist rest. However, these 10 hour coding days + this time writing the blog takes a toll.

It’s time for my Sunday night look into where I’m at. I’m 53% done with Rails according to my dashboard and 50 lessons from my Rails projects. I’m still on pace to start my projects on May 10th at a 5 lesson/lab/lecture per day pace. Mon, Thurs & Fri of this past week I spent <1:30 working so if I can get that back up over 2 on weekdays (closer to 3 ideally) and keep this solid 10 on the weekends I’ll be happy.

Time spent today: 10:11
Time spent total: 242:14
Lessons completed today: 9
Lessons completed total: 466

Get in Flow and Go

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 the has_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 the User 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 properties

PJ 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 class

PJ 15 MINUTES AGO
and then have different associations using the same source object

PJ 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 a trip_listing

Seth Alexander 10 MINUTES AGO
So are we basically “reaching” into trips and pulling out the listing method then setting it to trip_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 object

PJ 9 MINUTES AGO
which since user has many trips it also has whatever relationships come with it

PJ 8 MINUTES AGO
which is sort of the argument against using OO programming because when you have an object with relationships all of a sudden you have everything that it has

PJ 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
So trip_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 guest

PJ 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

Coding While Sleeping. Is That Possible?

I’m going to be very happy to code during the day tomorrow. I’ve been tired these last few nights during my target of 8pm – 11pm coding time. I just took a second, leaned back in my chair so my head could rest on the chair back, closed my eyes to think about a problem, and almost fell asleep.

Learned about some Active Record Callbacks of which there are many more than I would have imagined:

  1. Creating an Object
    • before_validation
    • after_validation
    • before_save
    • around_save
    • before_create
    • around_create
    • after_create
    • after_save
    • after_commit/after_rollback
  2. Updating an Object
    • before_validation
    • after_validation
    • before_save
    • around_save
    • before_update
    • around_update
    • after_update
    • after_save
    • after_commit/after_rollback
  3. Destroying an Object
    • before_destroy
    • around_destroy
    • after_destroy
    • after_commit/after_rollback

That’s all. Goodnight.

Time spent today: 0:56
Time spent total: 222:27
Lessons completed today: 1
Lessons completed total: 450

Demo Day & Join the FBI?

I’m exhausted tonight. Been working mainly on more Active Record Associations. More complex ones than before I would assume to get in practice for later labs. Foreign key and Class Name Active Record class methods to be exact if you’re looking for some light reading lol.

I attended an Alumni career fair at my alma mater Belmont University today which was interesting. I never attended one as an undergrad so the whole, drop your resume off on as many tables as possible, was lost on me. I did get a chance to chat with more than a few HR people and get some leads. Nobody was actively hiring Junior Developers but I’m not job ready yet so that’s fine. I’m getting on the radar and will follow up with all of them tomorrow when I can actually type without errors. Also, the FBI recruiter informed me I should apply to be an Intelligence Analyst.

The more fun thing I did today was attend The Iron Yard‘s Spring Demo Day for their current graduating class. It was fun to see people’s projects and learn more about how they built them. I saw some neat stuff. It was also interesting how The Iron Yard splits their students into Front End and Back End tracks then have them work together on their final projects. A few of the guys I met said they might attend the FCC meetup on May 6th so I look forward to that.

Time spent today: 1:30
Time spent total: 221:31
Lessons completed today: 2
Lessons completed total: 449

Don’t Forget to Check the Password

Watching Avi build out the TodoMVC is an interesting thing and I’m happy I get to do it as part of my curriculum and showing what Rails does. That was the bulk of the new work I did tonight. Something that I ran into during my Sinatra project was getting all my associations to work properly. I had issues because of what I now know is called the Active Record Inflector. Now I have this handy table in my notebook:

Name Data
Model Author
Table authors
Foreign Key author_id
belongs_to :author
has_many :authors

This is going to make life easier, although, I’m pretty sure I’ll be building most of my migrations using generators in Rails. However, I’ll still have to fill in my models so this works as a helper for sure.

So… I went to a meetup tonight. I pretty loose one where we all just hung out and worked on whatever we needed to. I decided to work on making my Sinatra portfolio project even better. The largest thing I realized was that I hadn’t implemented authentication checking when people log in. So, I was checking that the username matched one in the db but I wasn’t checking to make sure the password was correct.

post '/login' do
  @user = User.find_by(username: params[:username])
  if !!@user
    session[:id] = @user[:id]
    redirect 'houses'
  else
   redirect 'login'
 end
end

So I added that in 🙂

post '/login' do
  @user = User.find_by(username: params[:username])
  if @user && @user.authenticate(params[:password])
    session[:id] = @user[:id]
    redirect 'houses'
  else
   erb :'users/login'
 end
end

I also realized that this will not give me any errors when the password is incorrect. At least not in the @user.errors way that I was getting errors due to validations not passing. I had to figure out a way to display a username/password is the incorrect message but only when the login fails. I solved that in the view.

<h1>Please Login:</h1>
<p>
  <% if !!@user %>
    <h3 style="color:red">Incorrect username or password.</h3>
  <% end %>
</p>

I still have some UX things to add in so navigation is easier from page to page instead of all of it happening for the most part from dropdowns in the navbar but I’m pretty happy with where the app is at now for being in my portfolio.

Time spent today: 2:59
Time spent total: 220:01
Lessons completed today: 2
Lessons completed total: 447

Strong Params Can Change

Not much going on but completing a couple larger labs tonight. One significant thing that halted me for a while was an issue in my strong params. I had:

def song_params
  params.permit(:title, :released, :release_year, :artist_name, :genre)
end

Which looks really nice but doesn’t take into account that I’m using form_for and thus my submitted data is actually a nested hash. A quick change to:

def song_params
  params.require(:song).permit(:title, :released, :release_year, :artist_name, :genre)
end

Had me moving along again. That’s all I have the energy to type tonight since my 4-month-old had me up at 6am this morning.

Time spent today: 2:38
Time spent total: 216:21
Lessons completed today: 2
Lessons completed total: 445

The Speaker-Listener Technique

I can happily say that I got zero, yes 0 coding done today. Well, I guess I got a little in during my 45 minutes Sinatra Project review this morning. Which was a great review btw.

Hi Seth,

Awesome job on your Sinatra Project!! You did a great job, and I really appreciate your work on the Active Record relationships and your clean and concise code. As you move forward these practices will help you a lot.

Please reach out if you have any questions or concerns as you move through Rails.

Regards,

A couple things that I shored up/added this morning were Active Record validations for uniqueness on username and email when someone signs up as well as displaying error messages on the sign-up page.

Why am I happy about this? Well on Sunday night my wife and I attended a brief marriage seminar. Just something to provide us with tools to communicate better (something most marriages probably need). Tonight we utilized the take home (a fridge magnet) and had a good conversation. So what does this fridge magnet say? It’s the “Speaker-Listener Technique”.

The Floor
Speaker-Listener Technique

Rules for the Speaker:
1. Speak for yourself. Don’t mind read!
2. Keep statements brief. Don’t go on and on.
3. Stop to let the Listener paraphrase.

Rules for the Listener:
1. Paraphrase what you hear.
2. Focus on the Speaker’s message. Don’t rebut.

Rules for Both:
1. The Speaker has the floor.
2. Speaker keeps the floor while Listener paraphrases.
3. Share the floor.

Needless to say, this concept worked very well tonight for my wife and I. It’s designed for times of conflict but we just used it for a normal conversation. Because of this, I’m happy I missed out on coding. While I miss it I know what’s truly important during this short life and a few hours of Rails is not #1.

Time spent today: 3:52
Time spent total: 213:43
Lessons completed today: 12
Lessons completed total: 443