SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Link attribure of models

    I have a problem
    I have a db with 2 tables

    MAILS: id, receiver_id,sender_id,message

    USERS: id, name

    How i can do to link the attributes of models: receiver_id and sender_id with the id of the user's table?
    For link i mean insert: belongs_to has_many...

    If there aren't the 2 attributes but only receiver_id i can rename receiver_id like user_id and so i can set models like this:

    class User < ActiveRecord::Base
    has_many:mails
    end

    class Mail < ActiveRecord::Base
    belongs_to:user
    end

  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)
    Try this:

    Code:
    class Mail < ActiveRecord::Base
      belongs_to :receiver, :class_name => 'User'
      belongs_to :sender,   :class_name => 'User'
    end

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response.
    do User class works so?
    class User < ActiveRecord::Base
    has_many:mails
    end

  4. #4
    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 might need to do it like this:

    Code:
    class User < ActiveRecord::Base
      has_many :received_mails, :class_name => 'Mail', :foreign_key => 'received_id'
      has_many :sent_mails, :class_name => 'Mail', :foreign_key => 'sender_id'


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
  •