Creating controllers and views for has_many: through relationships in Rails 3

There are many tutorials that show how to create model instructions for has_many: through relationships in Rails, but there seem to be few articles related to the process of setting up forms to create and edit these relationships. I'm looking for some help (or good examples) on how to create an interface that allows users to manage these types of relationships in a Rails application.

Here's the script:

I have users, relationships and athletes. The user can have Communication with the athlete in various roles: trainer, 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 create representations and controllers that will allow me to create a user-to-athlete relationship (with the role of coach, parent, etc.), edit the relationship, or destroy the relationship.

Ultimately, my goal is to create a scenario in which users can create athletes and choose related relationships.

Unfortunately, I cannot find any specific guides or links that give me much more than model instructions or an example has_many relationship.

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

+6
source share
2 answers

The relationship between you and your user and athlete model is essentially a has_and_belongs_to_many (HABTM) relationship. Turning back and forth, it seems you are embarrassed that the best way to create this relationship.

A good place to start reading is in the documentation for ActiveRecord associations, in particular the documentation for HABTM relationships .

You model setup is fine. Now that you have your HABTM relationship setup, here's what you can do. Suppose your Athlete and User model is very simple and has nothing but the name attribute, which is a string. Now you can do this code (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 above line will create a user named Mike and will automatically create a relationship record with the corresponding attributes to link them. So if you call this:

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

Now, if you want to allow the user to determine what the relationship between himself and the athlete is when creating the athlete, you can configure your relationship class to have a relation field of type string , and when creating relationships (as I just showed above), you can do something like that:

 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"> 

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

+5
source

Try railscasts or ascii casts. What I usually start. Not sure if this is what you need, but there is a tutorial on these sites for nested forms. I think it is in complex forms. Is it worth reading / watching at all?

0
source

Source: https://habr.com/ru/post/901078/


All Articles