SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    more related table issues

    I am trying to show data from a topics table that has a one to many relationship with a posts table. The following code only shows the the foreign key (expectedly.) How do I get this to show the topic in the topics table based on this foreign key?

    Thanks!

    Code:
    <%= post.topic_id %>

  2. #2
    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)
    You'll likely have to set up the associations in your models, so in post.rb:

    Code:
    belongs_to :topic
    and in topic.rb:

    Code:
    has_many :posts
    After that point you should be able to get your data as intended: post.topic.title or post.topic.description or what have you.

  3. #3
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually have belongs_to :topic in post.rb and has_many osts in topics.rb and have tried

    post.topic.topic

    My table is called topics and the only besides the id field is topic. And just for good measure I think I have tried every combination of that post.topics.title, post.topics_id.title

    Not sure what I am doing wrong. Thanks for your help

  4. #4
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you please provide us with the start of your post.rb, topic.rb, a description of your tables and where you're trying to make these calls?

    Code makes it much easier to help you
    Thanks
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.


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
  •