7

There are many tutorials that show you how to create the model instructions for a has_many :through relationship in Rails, but there doesn't seem to be many articles related to the process of setting up forms to create and edit these relationships. I am seeking some assistance (or good examples) of how to create an interface that will allow users to manage these types of relationships in a Rails app.

Here's the scenario:

I have Users, Relationships, and Athletes. A User can have a Relationship with an Athlete in a variety of roles: Coach, Mentor, Parent, or Fan.

Here are my models:

class User < ActiveRecord::Base
  has_many :relationships
  has_many :athletes, :through => :relationships
end

class Athlete < ActiveRecord :: Base
  has_many :relationships
  has_many :users, :through => :relationships
end

class Relationship < ActiveRecord :: Base
  belongs_to :users
  belongs_to :athletes
end

So, the next step is to build the views and controllers that allows me to create a User-to-Athlete relationship (with a coach, parent, etc role), edit the relationship, or destroy the relationship.

Ultimately, my aim is to have a scenario where Users can create Athletes and choose the associated Relationship.

Unfortunately, I can't find any specific tutorials or references that gives me much more than the model instructions or the example for a has_many relationship.

If anyone has a link or example that can solve this problem at a simple level, I should be able to customize the rest.

Randy Burgess
  • 4,835
  • 6
  • 41
  • 59

2 Answers2

4

The relationship you have here between your User and Athlete model is essentially a has_and_belongs_to_many relationship (HABTM). By going back and forth with you it seems you're confused about what the best way to create these relationships would be.

A good place to start reading would be in the documentation for ActiveRecord's Associations, specifically the documentation for HABTM relationships.

You're model setup is fine. Now that you have your HABTM relationship setup, here's what you can do. Let's assume both your Athlete and User model are very simple and have nothing but a name attribute, which is a string. You can now do code like this (this is console output from the rails console):

User.create(:name => "Jeff")
usr = User.first
=> #<User id: 1, name: "Jeff">
usr.athletes
=> []
atl = usr.athletes.create(:name => "Mike")
=> #<Athlete id: 1, name: "Mike">

The line above will create a user with name Mike, and automatically create a relationship entry with the appropriate attributes to link the two. So now if you call this:

usr.athletes
=> [#<Athlete id: 1, name: "Mike">]

Now if you wanted to allow the user to dictate what the relationship is between themselves and an Athlete upon Athlete creation, you could setup your Relationship class to have a relation field of type string, and when creating the relationships (as I just displayed above), you can then do something like this:

rel = usr.relationships.where(:user_id => usr.id, :athlete_id => atl.id).first
=> #<Relationship id: 1, user_id: 1, athlete_id: 2, :relation => nil>
rel.relation = "Friend"
rel.save
=> #<Relationship id: 1, user_id: 1, athlete_id: 2, :relation => "Friend">

Hopefully this is more helpful than my original answer. Let me know if you have any questions. And definitely be sure to look at the ActiveRecord Associations documentation I mentioned above.

Batkins
  • 5,547
  • 1
  • 28
  • 27
  • Yeah, I did look at that example and it was just not a great one from an explanation standpont; although it might be the only one to use. – Randy Burgess Nov 09 '11 at 03:21
  • I guess I'm a little unclear on how you want the site to work. Perhaps in the Athlete#Show page, the user view has something where they can press a button to trigger a form to show which allows them to submit a form with a text field where it would add a relationship and dictate what the relation is to the athlete in the text field? Sorry that's the best I can do without a more thorough explanation of the desired functionality. – Batkins Nov 09 '11 at 03:34
  • In a nutshell, I have to build a form where a User can create an Athlete and then indicate what role they want for the Relationship (coach, parent, etc). Rails forms seem straightforward for a Many-to-One association, but there's not many examples of how to build views and controllers for many-to-many (that I have found). I might be making this too hard, but I'm still trying to figure out the "rails way" of doing this stuff. I could do this in PHP in my sleep. – Randy Burgess Nov 09 '11 at 05:05
  • Your original question seemed to me to be asking about a good way of doing this in a UI sense... Are you asking how to do the coding or how to implement the UI? – Batkins Nov 09 '11 at 07:05
  • I'm asking about the controller, mostly, since I think I can figure out the view if I know how to reference all three relationships in the controller. – Randy Burgess Nov 09 '11 at 15:55
  • Ok, think I have a better understanding of what your question was now. I've reposted a new answer which I think more directly answers your question. – Batkins Nov 09 '11 at 16:49
  • Awesome. This is exactly what I was trying to figure out. Thanks for taking the time to assist. – Randy Burgess Nov 09 '11 at 19:45
0

Try railscasts or ascii casts. That's where I usually start. Not sure if this is what you're after but there's a tutorial on those sites for nested forms. I think it's under complex forms. Would e worth reading / watching anyway.

Jenny Blunt
  • 1,576
  • 1
  • 18
  • 41