I have a table called ''users''. I have a second table called ''comments''. The comments table is in need of _two_ columns that both reference different user ids within the ''users'' table (one column for the user id that originated the post, another column for the user id for which the message is intended for. Obviously I can''t create two columns named ''user_id''. How do I go about creating two columns that reference the same foreign column? TIA. Jim _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jim Jones wrote:> I have a table called ''users''. I have a second table called ''comments''. > The comments table is in need of _two_ columns that both reference > different user ids within the ''users'' table (one column for the user id > that originated the post, another column for the user id for which the > message is intended for. > > Obviously I can''t create two columns named ''user_id''. How do I go about > creating two columns that reference the same foreign column?class User < ActiveRecord::Base has_many :sent_comments, :class_name => ''Comment'', :conditions => "sender_user_id = #{self.id}" has_many :received_comments, :class_name => ''Comment'', :conditions => "recipient_user_id = #{self.id}" end class Comment < ActiveRecord::Base belongs_to :sender, :class_name => ''User'', :foreign_key => ''sender_user_id'' belongs_to :recipient, :class => ''User'', :foreign_key => ''recipient_user_id'' end --- c = Comment.find_by_id N ... puts c.sender.name ... s = c.sender c_of_s = s.sent_comments ... c_of_s.each do |comment| ... end> TIA. > > Jim > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
class Comment < ActiveRecord::Base belongs_to :author, :class_name => ''User'', :foreign_key => ''author_id'' belongs_to :recipient, :class_name => ''User'', :foreign_key => ''recipient_id'' end ... by making the class and the FK explicit, you can name things whatever you like.. - james On 11/21/05, Jim Jones <jjones-lzy1PqPdFF3QT0dZR+AlfA@public.gmane.org> wrote:> > > > I have a table called ''users''. I have a second table called ''comments''. > The comments table is in need of _two_ columns that both reference different > user ids within the ''users'' table (one column for the user id that > originated the post, another column for the user id for which the message is > intended for. > > > > Obviously I can''t create two columns named ''user_id''. How do I go about > creating two columns that reference the same foreign column? > > > > TIA. > > > > Jim > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >