SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru CompiledMonkey's Avatar
    Join Date
    Sep 2002
    Location
    Richmond, VA
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Showing one to many relationships

    * Edit: Title should read "one to many" *

    This afternoon I've been looking into how I can represent a user having many tasks. In .NET I'd just create a bridge table with the user_id and task_id values and query that. In Rails, I think I can use :has_many to do something similar.

    I stumbled across this site (http://wiki.rubyonrails.org/rails/pages/ForumExample) but it's not making a whole lot of sense to me. I can see that the virtual Topics table is something I'm probably after.

    Can anyone break this down, or does anyone have a better link describing how to do this?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd only need a join table if you are doing a many-to-many relationship. A one to many relationship simply needs a foreign key on the "many" table. So in your case your tasks table will have a user_id. Then your model would just be:

    Code:
    class User
      has_many :tasks
    end
    
    class Task
      belongs_to :user
    end

  3. #3
    SitePoint Guru CompiledMonkey's Avatar
    Join Date
    Sep 2002
    Location
    Richmond, VA
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok cool, so after some fun times with migrations I was able to add the column to my tasks table.

    Will scaffold not show this relationship visually? I am using dynamic scaffolding in my admin_task_controller as a simple CRUD interface so I don't have to write a lot of SQL statements during development. Anyway, is it possible for this relationship to show up?

    Also, for coding I guess I can say "task.user.user_name" (or whatever the proper syntax is) to get to the user of a task. Is that right?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's cleaner to have a name column in your users table instead of user_name so you can do task.user.name instead.

    There are scaffolding plugins that show relationships, but the standard scaffold doesn't. I insert data from the console (ruby script/console):

    Code:
    u = User.create(:name => '...')
    u.tasks.create(...)
    This is less work than installing and using a custom scaffold.

  5. #5
    SitePoint Guru CompiledMonkey's Avatar
    Join Date
    Sep 2002
    Location
    Richmond, VA
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I've been using migrations to load some test data. I guess I'll keep doing that.


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
  •