ActiveRecord: Reference same table twice

Hi,

I have 2 tables, “users”, and “user_comments”. I am trying to allow
users to leave comments on each other’s profiles (eg, think youtube like
this: http://www.youtube.com/profile_comment_all?user=google).

Here is a simplified view of my database schema:

mysql> describe users;
±----------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±----------------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| login | varchar(32) | YES | MUL | NULL | |
±----------------±-------------±-----±----±--------±---------------+
2 rows in set (0.00 sec)

mysql> describe user_comments;
±---------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±---------------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| body | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| user_id | int(11) | YES | MUL | NULL | |
| poster_user_id | int(11) | YES | | NULL | |
±---------------±-------------±-----±----±--------±---------------+
6 rows in set (0.00 sec)

mysql>

  • user_id in the user_comments table references stores which user’s
    profile was commented on (100% working)

  • poster_user_id contains the user.id of user who made the comment, and
    is supposed to be linked back to the users table. I have it being
    populated with the correct user id of the currently logged in user when
    they make the comment, however, I am having some trouble getting the
    tables to join together in ActiveRecord.
    I have spent an entire day trying to get has_one and belongs_to to make
    this second relationship work, but i’m not having any luck :(, can
    anyone help in making this second link?

Here is what I have working in my models:

user.rb

class User < ActiveRecord::Base
has_many :user_comments, :order => ‘created_at DESC’
end

user_comments.rb

class UserComment < ActiveRecord::Base
belongs_to :user
end

So far, this will allow me to get all comments associated with a user,
eg, “@user.user_comments”.

This is what I want to be able to do in my view, to get the person who
posted the comment:

view.rhtml

Comments

<% for comment in @user.user_comments %>
<%= comment.title %> Posted by <% comment.poster.login %>
<%= comment.body %> <% end %>

so, something like poster becomes an object that can be referenced from
the comment.

How do I acheive this?

Any help appriciated.

Jason

user_comments.rb

class UserComment < ActiveRecord::Base
belongs_to :user
belongs_to :creator, :class_name =>“User”, :foreign_key
=>“poster_user_id”
end

Then reference user.user_comment.creator.login.

See the api for ‘belongs_to’ for more on this.

ah, thanks, that worked. I was going about it totally the other way
around, trying to put a has_one on the UserComment class to say “has one
user for poster_user_id”. :oops:

Many thanks,

Jason

Brian H. wrote:

user_comments.rb

class UserComment < ActiveRecord::Base
belongs_to :user
belongs_to :creator, :class_name =>“User”, :foreign_key
=>“poster_user_id”
end

Then reference user.user_comment.creator.login.

See the api for ‘belongs_to’ for more on this.

you are probably going to want to add

class User < ActiveRecord::Base
has_many :user_comments

add the line below

has_many :posted_comments, :class_name => “UserComment”,
:foreign_key => :posted_user_id
end

so that you can get to the comments that were posted by a particular
user.