Ruby on rails 具有其他类型关联的Rails多态表

Ruby on rails 具有其他类型关联的Rails多态表,ruby-on-rails,ruby,activerecord,ruby-on-rails-3.2,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 3.2,我目前正在为Rails 3.2应用程序建模,我需要一个名为“archives”的表中的多态关联“archivable”。不用担心,但我的“归档”表也必须属于“连接”表。我只是想知道Rails是否有这样做的限制 另一个细节是,我的连接模型有两倍的用户id作为外键。用户标识作为发送方,用户标识作为接收方。可能的我想我在下面所做的是行不通的 这是我的模特协会 class User < ActiveRecord::Base has_many :connections, :foreign_ke

我目前正在为Rails 3.2应用程序建模,我需要一个名为“archives”的表中的多态关联“archivable”。不用担心,但我的“归档”表也必须属于“连接”表。我只是想知道Rails是否有这样做的限制

另一个细节是,我的连接模型有两倍的用户id作为外键。用户标识作为发送方,用户标识作为接收方。可能的我想我在下面所做的是行不通的

这是我的模特协会

class User < ActiveRecord::Base
  has_many :connections, :foreign_key => :sender
  has_many :connections, :foreign_key => :receiver
end

class Connections < ActiveRecord::Base
  belongs_to :user
  has_many :archives
end

class Archive < ActiveRecord::Base
  belongs_to :connection
  belongs_to :archivable, :polymorphic => true
end

class Wink < ActiveRecord::Base
  has_many :archives, :as => :archivable
end

class Game < ActiveRecord::Base
  has_many :archives, :as => :archivable
end

class Message < ActiveRecord::Base
  has_many :archives, :as => :archivable
end
class用户:发送方
有多个:连接,:外键=>:接收器
结束
类连接true
结束
类Wink:可归档
结束
类游戏:可归档
结束
类消息:可归档
结束
您是否发现Rails有任何错误或不可行的地方


谢谢大家。

我想你们应该这样做:

class Connections
  belongs_to :sender, :class_name => 'User', :foreign_key => 'sender_id'
  belongs_to :receiver, :class_name => 'User', :foreign_key => 'receiver_id' 
end

class User
  has_many :sended_connections, :class_name => 'Connection', :as => :sender
  has_many :received_connections, :class_name => 'Connection', :as => :receiver
end

重要提示:不要声明2次有多个:同名连接

谢谢你,伙计。关于我想得到的多态关联,不用担心这个设计?一点补充:不是
User.to\s
Connection.to\s
我会分别使用“User”和“Connection”。原因是,通过使用类常量,您强制rails/activesupport立即需要另一个模型,这可能会导致循环依赖关系。@MarianTheisen哦,是的,您是对的!我在我的应用程序上做了一些工作来纠正它。。。