Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances
I need to create a very simple web app:

- A "Team" which contains main contact information
- A "Participant" which contains a particular person

The Team has_many participants and the participant belongs_to Team

It's a simple one-to-many relationship

All I really need the app to do is add those together as one step. i.e. Create a team and add the participants.

So far what's stopping me is how to implement the one-to-many in Rails.

Does anyone have a sample application that implements a one-to-many and has views that allow them to be created at once? Should I be combining the two above into one Model? Or should they be two separate models with a single controller?

Adbot
ADBOT LOVES YOU

roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances
Thanks, after quite a bit of trial and error I've got it functional.

Now to make it usable with the magic of error handling and stylesheets :)

roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances
Say I have the following models:

Person
Job

With data:

Person:
- John
- Tony
- Bob

And Jobs:
- Driver
- Shipper
- Receiver
- Cook

When I'm creating the actual class for the model, I would have for Person:

code:
fields do
   name                 :string
   description       :text
   timestamps
end

has_many :Jobs
To show that each person can have many jobs and vice-versa in Job to show that each Job can be done by many Persons. You know, a many-to-many relationship.

Now my question is - what if each person has multiple primary Jobs and multiple secondary Jobs? How would that be expressed?

Would I need to create a third model called, for example, PrimaryJob that has a Person and Job? Then another one called SecondaryJob with Person and Job indexes?

I figure that would work but seems that there should be a way to do that within the Person/Job model without creating the extra model.

roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances

skidooer posted:

If jobs are unique there is no point in the added complexity of a join model.
That's the whole thing, jobs are not unique and each person has primary jobs and secondary jobs.

For example:
John, primary jobs: driver, cook, secondary jobs: shipper
Tony, primary jobs: shipper, secondary jobs: cook, receiver
Bob, primary jobs: driver secondary jobs: receiver

Need to the simplest way to express a many-to-many relationship multiple times that reference the same model.

roop
May 10, 2002

I am become Roberto, the destroyer of scoring chances

trinary posted:

Read up on has_many :through and do something like what I suggested. :)
Aahh, a light bulb just went on in my head and it all makes sense now, thanks. This helped as well:

http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply