SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Help on Many-to-Many relationship

    I'm just completely stuck on this...I'm sure their is a slick way of doing things but I just can't seem to find a solution..even through a google!!!

    In advance, thanks for replies!

    Problem:
    Assume I have a table called people, people_skills, and skills.

    class Person < ActiveRecord::Base
    has_many : people_skill

    has_many :skills, :through => : people_skill
    end

    class PeopleSkill < ActiveRecord::Base
    belongs_to : person
    belongs_to :skill
    end

    class Skill < ActiveRecord::Base
    has_many : people_skills
    has_many : people, :through => : people_skills
    end

    The above code works and I can save Skill rows from Person object.
    Example
    skill = Skill.new
    skill.name = "watching tv"
    person = Person.find(1)
    person.skills << skill
    person.save

    Again, the above code works.

    Here is where I'm having trouble. When I use
    <&#37;= link_to 'New skill', new_skill_path %>
    Obviously, it's going to a page where I can create skill but I want to associate that to person. I've tried this
    <%= link_to 'New skill', new_skill_path(person) %> but no go. Thanks for helping RoR noob.


    <% for person in @people %>
    <table border="2" width="100%">
    <tr >
    <td align="left"><b>Personal Name</b>: <%=h person.name %><br/>
    <b>Personal Info</b>: <%=h person.info %></td>
    <td align="right">
    <%= link_to 'Delete This Person', person, :confirm => 'Are you sure?', :method => :delete %><br/>
    <a href="#">Upload Resume</a>
    </td>
    </tr>

    <tr>
    <td>

    <% for skill in person.skills %>
    <%=h skill.name %>
    <br />
    <% end %>
    <%= link_to 'New skill', new_skill_path %>
    </td>
    <td>

    </td>
    </tr>
    </table>
    <p />
    <% end %>

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I intentionally put space between : and p, it was giving smiley face before

  3. #3
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Did you set up the map.resources route in your routes.rb file?

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be able to replace:
    Code:
    class Person < ActiveRecord::Base
      has_many :people_skill
      has_many :skills, :through => :people_skill
    end
    
    class PeopleSkill < ActiveRecord::Base
      belongs_to :person
      belongs_to :skill
    end
    
    class Skill < ActiveRecord::Base
      has_many :people_skills
      has_many :people, :through => :people_skills
    end
    With
    Code:
    class Person < ActiveRecord::Base
      has_and_belongs_to_many :skills
    end
    
    class Skill < ActiveRecord::Base
      has_and_belongs_to_many :people
    end
    You don't have to define a class for the join table. ActiveRecord will automatically recognise that there is a join table with a name made by combining the other two table names in alphabetical order (so your join table name is right: people_skills).

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's very cool! I knew there's shorter way. But, I don't mean that part. Let me try to rephrase my problem.

    Let say I have a HTML page that lists all person as a table and within that table there's a link to add "Skill". How would I generate that link?
    <&#37;= link_to 'New skill', new_skill_path %>
    Obviously, the above link will go to a page that will add skills but will not insert a row into the join table. I'm thinking something like
    <%= link_to 'New skill', new_skill_path(person) %> would work but obviously I'm getting an error. Hopefully, this clears up on the issue I have.

  6. #6
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a look at these and tell me if they help you:

    Handling Many-to-Many Relationships in Rails
    Forum post about has_many through or has and belongs to many

    What you're basically after (if I understand you correctly) is a way to assign a new skill to a person from the edit person action/view. The first reference I listed above shows doing that with a set of checkboxes (skills the person has checked, check other boxes to add those skills).

    You could also use other form controls to do this, such as option/select or a pick list. I don't know your AJAX/RJS skills, but you could write a web service for that and do a link_to_remote to add the skill when it's selected from the select/option menu, with another one to delete the skill.

    Hope I've helped.

    Code:
    :p
    Try code tags to get around the smileys
    Last edited by Arlen; Nov 7, 2008 at 19:24. Reason: Fatfingers

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thank you Arlen, it's exactly what I'm looking for!

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,185
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Arlen
    Try code tags to get around the smileys
    Off Topic:

    Or simply check "Disable smilies in text"

    :< :( :p

  9. #9
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but since it was code he was posting that the smileys showed up in, code tags seemed more logical.

  10. #10
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The other reason why using code tags is better is that indenting is preserved within the code tags, so your code is easier to read.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •