0

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

你可以在这里看到模型

另一个细节,我的 Connection 模型有两次 user_id 作为外键。一个 user_id 作为发送者,一个 user_is 作为接收者。可能的?我认为我在下面所做的将行不通...

这是我的模特协会。

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

您是否发现 Rails 有什么问题或无法解决的问题?

谢谢你们。

4

1 回答 1

1

我想你想这样做:

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 次 has_many :connections 同名!

于 2013-04-18T10:36:46.060 回答