不能把我的头缠绕在这个...

class User < ActiveRecord::Base
  has_many :fantasies, :through => :fantasizings
  has_many :fantasizings, :dependent => :destroy
end

class Fantasy < ActiveRecord::Base
  has_many :users, :through => :fantasizings
  has_many :fantasizings, :dependent => :destroy
end

class Fantasizing < ActiveRecord::Base
  belongs_to :user
  belongs_to :fantasy
end

...这适合我的主要关系,因为 User 能够 许多 Fantasies, ,那 Fantasy 能够 属于 太多 Users.

但是,我需要添加另一个关系 喜欢 (如, User “喜欢” Fantasy 而不是“拥有”它...想想Facebook以及您如何喜欢“墙壁柱”,即使它不属于您……实际上,Facebook示例几乎正是我M的目标)。

我收集到我应该建立另一个关联,但是我对如何使用它有点困惑,或者这甚至是正确的方法。我首先添加以下内容:

class Fantasy < ActiveRecord::Base

  ...

  has_many :users, :through => :approvals
  has_many :approvals, :dependent => :destroy
end

class User < ActiveRecord::Base

  ...

  has_many :fantasies, :through => :approvals
  has_many :approvals, :dependent => :destroy
end

class Approval < ActiveRecord::Base
  belongs_to :user
  belongs_to :fantasy
end

...但是我如何通过 Approval 而不是通过 Fantasizing?

如果有人可以直接对此设置,我将是很多义务!

有帮助吗?

解决方案

保留第一组代码,然后在用户模型中添加:

has_many :approved_fantasies, :through => :fantasizings, :source => :fantasy, :conditions => "fantasizings.is_approved = 1"

在您的幻想表中,添加IS_APPRADED布尔字段。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top