Deeply Understanding Active Record Associations the Ruby on Rails Way

The purpose of this blog post is to describe Ruby on Rails associations deeply.

Setting up the Practice Environment

First off, I’ll create a new application for practice. I do this on the desktop as it’s an easy place to work from:

rails new association_practice

Next step is to change directories so I’m on the root level of the new application.

cd association_practice

Next, I’ll create an association to practice with. I’m going to start with planet Earth. With Earth as a starting example, I can make associations with most Earthly things.

rails g model Planet name:string

This will generate files for the migration, model, tests and fixtures. I added two planets to the fixture file and a simple test. Which you can see in the following screenshot:

Now that we have the Planet object in our system, I’m going to add the next level of association, the Continent:

rails generate model Continent name:string

This command orders the Rails app to generate four more files. I’ve added to them a little and you can see the changes in the following screenshot:

Ruby on Rails Associations

The goal of this section will be to clearly explain how to use associations to organize your web application’s data.  The association documentation on the Ruby on Rails guides is a good start, but my descriptions below I hope will help deepen your understanding of associations and how to use them.

Understanding belongs_to and has_many Associations

Continents and Planets

Let us create our first association between the continents and the planets. Each continent can belong to a single planet, but a planet can have many continents. Here’s how we assemble it:

Next we migrate the database.

rails db:migrate

Next we load the fixture data.

rails db:fixtures:load

Awesome. Now we have our fixture data loaded and our associations should be set. Here is a screen shot of the actions we can now take in the rails console.

rails c

  1. Here we search the database for a planet with the name Earth. That gives us an array with the matching result in it. We use the .first method to pull the first object. We assign this object to the variable name earth.
  2. Here we use our earth variable to get the name string data which contains the name of the planet (“Earth”).
  3. Here we list the continents associated with earth.
  4. Here we set antarctica variable to equal the continent object of our first continent, Antarctica.
  5. Here we ask the database, “What planet is associated with Antarctica?” It returns us the earth planet object. Notice that the Planet earth object has the same id at the bottom as the Earth at the top.

Work in Progress

I’m thinking of better ways to describe associations. I plan to deep my understanding of the following associations in the future:

  • has_one Association
  • has_many :through Association
    • I’m thinking of describing this by using International Organizations
  • has_one :through Association
  • has_and_belongs_to_many Association
    • I’m thinking of describing this by using members of NATO
  • Choosing Between belongs_to and has_one
  • Choosing Between has_many :through and has_and_belongs_to_many
  • Polymorphic Associations
  • Self Joins

How to Become a Sea Plane Pilot with Fred Welsh

In this podcast Fred describes how he became a sea plane pilot. As the host, I know almost nothing about the subject. Therefore, we approach the subject from the perspective of a novice. I hope you find this useful if you’re seeking to become a recreational pilot. Enjoy!

fred and his sea plane

Fred Welsh is a friend I met in 2010 when I first visited Australia. On that trip he taught me to surf and showed me a great time in Yamba, Australia. He is awesome.

sea plane parked in the sea

How to Become a Sea Plane Pilot Time Stamps

  • 6:50 – History & Categorization of Recreational Pilots Licensing
  • 8:30 – Understanding the Cost and Process of a Recreational Pilots License
  • 15:10 – On the Economics of Purchasing an Airplane
  • 17:00 – The Benefits of a Sea Plane
  • 24:30 – Thoughts on Securing, Parking and Operating a Sea Plane
  • 28:30 – Sea Plane Endurance
  • 31:12 – Radio Communication Endorsement
  • 33:20 – Why Fly?
  • 40:00 – On Sea Plane Speed
  • 40:30 – Why a Sea Plane is Better than a Trailer/RV
  • 42:20 – Mapping out Sea Plane Adventures
  • 48:17 – Can You Do Work on Your Own Plane? – Maintaining a Sea Plane
  • 54:07 – Can Australia Sea Plane Pilots Fly Internationally?
  • 1:06:04 – Where You Inspired by Jimmy Buffett?
  • 1:16:40 – Lifestyle Questions – Weddings in Australia
  •  1:17:15 – The First Step to Becoming a Pilot

bobble head sea plane view

We call that tiger county. It’s when there isn’t anything down there to land on and you just hope nothing goes wrong

two sea planes at rest

It is a life or death situation. If you run into a huge storm, it can put you down hard. If in doubt, you stay home.

sea plane view of a whitsunday island

The pay-off for me is… I’ll say to my wife, “I think the whales are breaching today, let’s fly out and have a look.” You see sharks and dolphins… the rewards outweighs the costs of getting a license and a plane.

sea plane landing in the sea

When you’re flying long distances, you want to make sure you have plenty of room where you don’t have to get back for something.

two sea planes over the sea

Out of all of Australia, there are only 100 sea plane pilots. Of that, only 70 are operational. It’s a very, very small fraternity of pilots.

sea plane view of whitsunday islands in australia

The beauty of my category is that they have made the books more easy to understand for laymen. I found it enjoyable and not overly difficult. I found the process of learning to be exhilarating.

sea plane view of the gold coast

Related Links

sea plane view of the beautiful whitsunday islands

A Note on the Sound Quality Of This Podcast

I apologize for the poor sound quality on this one. Fred and I spoke while he was in Australia and I was in the United States. We suffered during the conversation because there was a second delay from us speaking to when the other would hear. Also, I failed to record split tracked. None-the-less, I think the conversation was interesting enough to publish despite the low quality.

I’ve sorted it out. Next episode will be better.

Episode One of the Question Mark Podcast

Welcome to ? a podcast about art, adventure, enterprise and more.

Subscribe on Apple Podcast

Episode art for question mark podcast one

This first episode is me my attempt at a unique podcast. Currently, I have three formats in mind:

1. Traditional Interviews

Traditional interviews will be around an hour where we learn about interesting people who have interesting behaviors. These interviews will focus on the subjects of art, adventure and enterprise.

2. The Linked Series

The idea for the linked story started on the Nomad Cruise while I was talking with my friend Becky. We parted ways in Dubai and she took one of my microphones. We did a podcast while she was in Chiang Mai and I was in Bangkok. Now that our conversation is complete, Becky will pass the microphone onto someone who she finds interesting.

The great thing about this is that the guest selection will be entirely up to Becky. Once Becky hands the microphone to someone else, that person will be the next person to be in charge of guest selection.

I hope they pick interesting guests.

3. ? Riffs

Question mark riffs are segments where I splice together a collection of interesting audio based on a theme. I’ll attempt to provide some context and commentary to link them together.

For instance, I hope to do one on investing in which I can pull audio from people like Ray Dalio, Warren Buffett and Mark Cuban. Other topics could include:

  • Surfing
  • International relations
  • Mountain climbing
  • Style
  • War
  • Country Reports

…or whatever else I become inspired by.

The following podcast is a rough example of how a ? riff would sound like:

Join the Conversation

I’d love to hear from you. Please feel free to click the image below and you’ll be able to record a voice message link for the show. I’d be honored to hear from you.

In my last podcast (the Love Affair Travel Podcast), I never heard back from the audience. That was a huge problem. My statistics were telling me that around 1,500 people were listening to each podcast episode, but I never heard from anyone. I think that is largely responsible for why I stopped creating them.

If you’d like to be part of the solution, please let me know who you are and where you are listening to the show. You can either email me an .mp3 file ([email protected]) or you can click the image below:

record a message for the podcast banner

Episode Credits

Tribute Page to Jimi Hendrix – Responsive Web Design Project

In this tutorial, I work through creating a tribute page as part of the responsive web design project requirements for Free Code Camp. I completed this project a long time ago, but this time it’s different. This time, I record my process for writing the page and pushing it to production.

I think this is a good tutorial for combining the things we learned in the responsive web design certification curriculum. That covers basic CSS and HTML. This is a beginner project, but it provides the path to go from writing your first HTML code to pushing it to the internet.

A little more….