0

我有一个模型用户。用户可以拥有一个对象。用户还可以相互发送对象。我正在尝试访问用户的所有“发件人”,以便我可以访问 @user.senders 并获取该用户的所有唯一发件人。这是我的设置:

class Object < ActiveRecord::Base
  belongs_to :user

  has_many :senders, through: :object_relationships, source: :user, class_name: "User"
  has_many :object_relationships
end

class User < ActiveRecord::Base
  has_many :owned_objects, inverse_of: :user

  has_many :objects, through: :object_relationships
  has_many :object_relationships
end

class ObjectRelationship < ActiveRecord::Base
  belongs_to :user
  belongs_to :object
end

有任何想法吗?

4

1 回答 1

0
class Gift < ActiveRecord::Base
  belongs_to :user

  has_many :gift_senders    
  has_many :senders, through: :gift_senders, source: :user
end

class User < ActiveRecord::Base
  has_many :sent_gifts, class_name: 'Gift', foreign_key: :user_id

  has_many :gift_senders    
  has_many :accepted_gifts, through: :gift_senders, source: :gift
end

class GiftSender < ActiveRecord::Base
  belongs_to :user
  belongs_to :gift
end

为了避免混淆,我已将您的模型重命名为礼物。

于 2013-02-03T00:05:49.887 回答