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 ---------- <h3>Comments</h3> <% for comment in @user.user_comments %> <hr /> <b><%= comment.title %></b> <i>Posted by <% comment.poster.login %></i> <br /> <%= 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
# 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. On 11/27/06, Jason <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 > ---------- > <h3>Comments</h3> > <% for comment in @user.user_comments %> > <hr /> > <b><%= comment.title %></b> > <i>Posted by <% comment.poster.login %></i> > <br /> > <%= 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 > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Hogan 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 11/27/06, Jason <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 Hogan 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. > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---