Quick Ruby on Rails Question
If there are any Rails people out there, I have a quick question:
Say I have a table for players (player_id and name) and a table for teams (team_id and name). Now, a player belongs to a team, and a team has many players.
I use migrations to generate those two tables, including the “_id” of each other in them.
I generate the scaffold for the two tables.
But when I go to edit a player, I can’t specify which team he’s on. When I go to edit a team, I can’t specify which players belong on it.
Does the scaffolding not handle that? Is there a third scaffolding I need to create? I have a funny feeling I need to specify players_teams to get something going here. Or is that just in a many-to-many relationship?
Or, basically, do I just have to skip the scaffolding and code it in myself? Scaffolding won’t do it.
Help? And thanks.

March 30th, 2007 at 1:08 pm
Hey Augie, I highly recommend that you just skip the scaffolding stuff and do it yourself. I’ve been doing Rails development for about a year and half and I only used scaffolding for about a month of that time. You will learn much more about ruby and rails if you code it yourself.
I just glanced at your problem but I think you will need to use has_and_belongs_to_many
http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many
March 30th, 2007 at 1:14 pm
Thanks, Kevin. It is my intention to get off the scaffolding crutch as soon as possible. I just want to make sure everything else I’ve set up around the scaffolding is functioning properly. It’s worrying to throw a team_id line into the bowlers table and not have it show up in the scaffolding, you know? I need to play with that tonight to see if I can add it myself and get data into the backend database that way.
Besides which, the stuff scaffolding comes up with is VERY bare bones. It’s impressive for about the first thirty seconds, then you want to get into the RHTML and the ERB and the CSS and go nuts.
March 30th, 2007 at 1:23 pm
If a player can only belong to one team then you don’t need has_and_belongs_to_many. You can just add has_many :players to the team model. I don’t much about scaffolding so I’m not sure if you could just add that to the model and make it work.
March 30th, 2007 at 2:03 pm
Yeah, for now it’s a one team per player thing. If this thing ever went far enough and got specific enough, there’d be the chance for players to play on different teams in different leagues, switch teams, etc. etc. But for the purposes of learning the language, I’ll just do this one step at a time and iterate as needed.
I’m sure another instance of a many-to-many relationship will arise naturally as things get more complicated.
April 14th, 2007 at 10:52 pm
[…] But I did luck across an answer to a question I asked here a couple of weeks back. That is, why won’t my relations between objects in my database show up in the scaffolding? I found it while reading SitePoint’s RoR book: However, scaffolding does have its limits, as I mentioned earlier. For example, it can’t cope with ActiveRecord associations such as “a Story belongs to a User,” which we saw in Chapter 4. Additionally, since most applications end up requiring a fully fledged administrative interface, you’re often better off just creating the real thing rather than fiddling around with a dummy interface. […]