SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting string value from third table

    I have three tables person, person_issues and issues. Issues table is a list of issues and they are stored in the person_issues table as issue_id. I know you use the following construct to get the issue_id
    Code:
    <% @person.person_issues.each do |k| %>
    			<tr>
    				<td class="heading-subtle"><%= k.issue_id %></td>
    </tr>
    <% end %>
    But how do I get the actual name from the issues table instead of the issue_id from the people_issues table?

    Rick
    Ruby, Ruby when will you be mine

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the field is "name" then I think this should work

    k.issue.name

  3. #3
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, that did not work and I tried several variations on your suggestion. The issues are stored in a table called issues with fields id, issue, description if that helps. When I iterate through the person_issues table I can get the "issue_id" value but not the string value of the field "issue" which is what I am after. in the issue.rb file I have belongs_to :person_issue and in the person_issue.rb file I have belongs_to :person and has_many :issues.

    I hope this helps.

    Rick
    Ruby, Ruby when will you be mine

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ricklach
    in the issue.rb file I have belongs_to :person_issue and in the person_issue.rb file I have belongs_to :person and has_many :issues.
    This looks wrong to me. If I'm reading the situation correctly, at minimum, has_many :issues should probably be belongs_to :issue. At maximum, you should ditch the PersonIssue model and use has_and_belongs_to_many.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may be correct but I think the table structure is correct. To recap in more detail: the person table provides an id to the person_issue table and the issues table, which is only a look-up table, provides an issue id to the person_issue table. So the person_issue table is just a collection of people and issues. The basic structure is like this: id, person_id, issue_id, commitment and comment plus a few other bookkeeping fields. In the issues table I have id, issue, description. So if I am correct, the person_issues.rb file "has_many :issues" and the issues.rb file "belongs_to :person_issues".

    Now going one step further, in the person_controller I have the following:
    Code:
    @person = Person.find @params['id'], :include =>[ :residence, :memberships, :donations, :person_issues ]
    All of the actions for memberships, donations and person_issues take place in popups. However, when I edit the person's profile I iterate through the memberships, donations and person_issues files to produce a table on the main edit screen. I can get the person_issue.issue_id ok but I want to really have the string value of what issue_id is equal to (called issue) in the issues table. Thus far, I have everything working as it should except that I cant get the string value of issue. So I suspect it is something in my model definition that I am doing wrong but I have tried numerous experiments and the best I can get is an error. Hope this description helps describe the problem adequately.

    Rick
    Ruby, Ruby when will you be mine

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Usually when you see a pair model1_model2 table, it is relating one model1 to one model2. The data in this table may be:
    Code:
    politician | lie
             1 | 1
             1 | 2
             1 | 3
             1 | 4
             1 | 5
             2 | 3
    In this example, politician one has told many lies, but each row in the table only reflects one lie. Also, each lie may be told by many politicians, but again, each row only represents a connection between one politician and one lie.

    Am I mistaking how you are doing this?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I think you have described what I am trying to do. In my case one person can have many issues (just like real life ) like:
    Person table | person_issue table | issue table
    person | issue_id person_id | issue(look-up table for drop down box)
    1 | 3 1 | Smoking
    1 | 1 1 | Global Warming
    1 | 5 1 | Health Care
    2 | 6 2 | fishing
    2 | 3 2 | Smoking

    So it is the value in the right-most table that I want to capture as opposed to the issue_id (which I can easily access) in the middle table (plus other not shown values). I don't really think this calls for a many to many table. I Hope this describes what I want.

    Rick
    Ruby, Ruby when will you be mine

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seriously, this sounds like what you want:
    Code:
    class Person < ActiveRecord::Base
      has_and_belongs_to_many :issues
    end
    
    class Issue < ActiveRecord::Base
      has_and_belongs_to_many :people # (maybe persons)
    end
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I will give that a try and let you know how it works but what happened to the person_issue table - the issue table is only a list of issues and their descriptions(this would be a snap in php). However, while pondering this problem I also thought of an ini type solution of name-value pairs to populate drop-down boxes. There are several problems for me though - the greatest of which is I don't know how to proceed. Do you know of any ror examples that would let me create ini-like files or is this a bad idea?

    Rick
    Ruby, Ruby when will you be mine

  10. #10
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ricklach
    Ok, I will give that a try and let you know how it works but what happened to the person_issue table - the issue table is only a list of issues and their descriptions(this would be a snap in php).
    has_and_belongs_to_many automagically looks for the person_issue table (actually, it will look for an issue_person table, it goes alphabetically by default so you will need to rename the table or specify its name). Then it will automagically look for a column named person to relate to "id" in the people table and a column named issue to blah blah blah. It's all in the documentation as well as in some turtorials, probably.

    If you add any extra data to the relationship itself, has_and_belongs_to_many won't break, but it also won't be able to help you with that.

    Rails has this nasty habit of jumping ahead of you. You go to solve a problem step by step and step one throws you the entire way up the ladder. Has and belongs to many is a good example, and, in the realm of select drop downs, options from collection for select is another.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  11. #11
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so far so good. Now I can get the issue name but how do I retrieve the values in the issues_person table (in other words I just reversed the problem - I can get the issue string but now I can't get the other values in the issues_person table). I just finished looking at the following example http://wiki.rubyonrails.com/rails/pa...elongs_to_many and it is the second example that I am trying to accomplish which essentially says it can't be done with HABTM. So time to rethink the problem.

    Since I wrote this I have found a solution. I simply created an issues object in the controller and in the edit screen where I display the table of issues I simply iterated over the issues object and printed out the result when it was equal to the issue_id. That solves the problem. Thanks for your suggestions.

    Rick
    Last edited by ricklach; Mar 26, 2006 at 13:43.
    Ruby, Ruby when will you be mine


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
  •