Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Ruby on Rails - Joining Tables

I am very new to ruby on rails, I have been following allong with most of the videos but I ran into a question. I have 2 tables set up:

My Shows have:
ShowID
Name
Fanart (url)
Poster (url)

My Actors have:
ActorID
SerieID
Name
Role
Image (url)

On Actors I want to be able to match Actor.SerieID to Show.ShowID to pull down show.name, show.poster, and show.fanart

On Shows I want to be able to match Show.ShowID to Actor.SerieID and find all the actors in that show pulling all of their information

I am not sure if I have given enough information. And if you just want to point me in the right direction like a tree house video or whatever that would be great to. Thank you for all your help.

5 Answers

I don't know if you've already found the answer or not, but to join tables you have to form associations. Hopefully when you made those tables, you made them through generating a rails model. If you followed the treehouse tutorials, this is probably what you did. If you just generated migrations instead of creating models, then I would generate a new migration, drop the tables, and then recreate your tables through models. So you'd have your Actors.rb file, and your Shows.rb file ready to edit under the "Models" folder.

So next you want to establish their relationships to each other. You'll need to use the belongs_to and has_many associations. In your case, I would use the Actor as belonging to the Show. It could really go either way around, but it seems as though your goal is more focused on finding information about a particular show. So your files might look like this:

      class Shows < ActiveRecord::Base
           has_many :actors
      end

      class Actors < ActiveRecord::Base
           belongs_to :shows
      end
    ```


This way you can now set up your controller files with variables and methods to define how the two tables will interact, and Rails will manage the associations from there on. The documentation on associations is really solid (http://guides.rubyonrails.org/association_basics.html).

Thank you so much Robert. Ya I am playing around with it right now. I think 'has_many' is exactly what I was looking for. I am going to go over the ruby guide that you posted. Thank you for the code and link, you have pointed me in the right direction and now I'm off to learn some more.

One more question that I may find the answer for in the rails guide but thought I would shoot it out there. Can I stack my has_manys? Like I have

Shows has_many Seasons has_many Episodes

also stack them like

Show has_many Seasons & has_many actors

Absolutely. You can associate almost anything you want. You could say Episodes has_many Actors, and make actors belong_to Episodes, and then Seasons would has_many Episodes, therefore the association between Seasons and Actors can be made by using the through association, which is incredibly awesome sauce.

So you could say something like,

```ruby
class Seasons < ActiveRecord::Base
       has_many :episodes
       has_many :actors, through: :episodes
end

class Episodes < ActiveRecord::Base
       belongs_to :seasons
       has_many :actors
end

class Actors < ActiveRecord::Base
       belongs_to :episodes
end

man I am really bad with outlining the code in this forum haha. I can never get it just right.

Your on fire, thanks again Robert.