本文介绍了has_one有两个外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类Message和User。消息具有sender_id和recipient_id这两个用户的外键。如何建立关系,我将能够获得发件人和收件人的用户,如@ message.sender.name和@ message.recipient.name



我试过这样做:

  class Message< ActiveRecord :: Base 

belongs_to:sender,:class_name => 'User',:foreign_key => 'sender'
belongs_to:recipient,:class_name => 'User',:foreign_key => '收件人'

结束

类用户< ActiveRecord :: Base

has_many:recivied_messages,:class_name => 'Message',:foreign_key => 'recipient'
has_many:send_messages,:class_name => 'Message',:foreign_key => 'sender'
end

但是没有帮助,当我试图访问,例如,@ message.recipient.name它说未定义的方法名称

解决方案

您可以使用:class_name 属性来设置哪个类用于外键:

  class Message< ActiveRecord :: Base 
has_one:sender,:class_name => User
has_one:recipient,:class_name =>用户
结束

类用户< ActiveRecord :: Base
belongs_to:sent_messages,:class_name =>消息
belongs_to:received_messages,:class_name =>消息
end

另外,你说你正在使用 sender_id recipient_id 作为外键,但是在你的代码中你有:foreign_key => 'sender':foreign_key => 收件人 。您是否尝试将它们更改为:foreign_key => 'sender_id':foreign_key => recipient_id ?所以:

  class Message< ActiveRecord :: Base 
has_one:sender,:class_name =>用户,:foreign_key => 'sender_id'
has_one:收件人,:class_name =>用户,:foreign_key => 'recipient_id'
结束

class用户< ActiveRecord :: Base
belongs_to:sent_messages,:class_name => Message,#... etc
belongs_to:received_messages,:class_name => Message,#... etc
end


I have two classes Message and User. Message has sender_id and recipient_id both foreign keys for User. How to build relationship where I'll be able to get user for both sender and recipient, like @message.sender.name and @message.recipient.name

I tried to do it by this way:

class Message < ActiveRecord::Base

  belongs_to :sender, :class_name => 'User', :foreign_key => 'sender'
  belongs_to :recipient, :class_name => 'User', :foreign_key => 'recipient'

end

class User < ActiveRecord::Base

  has_many :recivied_messages, :class_name => 'Message', :foreign_key => 'recipient'
  has_many :send_messages, :class_name => 'Message', :foreign_key => 'sender'
end

But it didn't help, when I'm trying to access to, for instance, @message.recipient.name it says that "undefined method `name'"

解决方案

You can use the :class_name property to set which class gets used for a foreign key:

class Message < ActiveRecord::Base
  has_one :sender, :class_name => User
  has_one :recipient, :class_name => User
end

class User < ActiveRecord::Base
  belongs_to :sent_messages, :class_name => Message
  belongs_to :received_messages, :class_name => Message
end

Also, you say you are using sender_id and recipient_id for the foreign keys, but in your code you have :foreign_key => 'sender' and :foreign_key => 'recipient'. Have you tried changing them to :foreign_key => 'sender_id' and :foreign_key => 'recipient_id'? So:

class Message < ActiveRecord::Base
  has_one :sender, :class_name => User, :foreign_key => 'sender_id'
  has_one :recipient, :class_name => User, :foreign_key => 'recipient_id'
end

class User < ActiveRecord::Base
  belongs_to :sent_messages, :class_name => Message, # ...etc
  belongs_to :received_messages, :class_name => Message, # ...etc
end

这篇关于has_one有两个外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:45