0

Profileモデルを介して接続された自己参照モデルがありRelationshipます。

class Profile < ActiveRecord::Base
  belongs_to :user

  has_many :accepted_relationships, class_name: 'Relationship', foreign_key: 'responder_id'
  has_many :followers, through: :accepted_relationships, source: 'initiator'

  has_many :initiated_relationships, class_name: 'Relationship', foreign_key: 'initiator_id'
  has_many :followed_profiles, through: :initiated_relationships, source: 'responder'

  has_many :groups
end

class Relationship < ActiveRecord::Base
  belongs_to :responder, class_name: 'Profile', foreign_key: 'responder_id'
  belongs_to :initiator, class_name: 'Profile', foreign_key: 'initiator_id'
  belongs_to :group
end

class Group < ActiveRecord::Base
  belongs_to :profile

  has_many :relationships

  attr_accessible :name
end

問題は、結合モデルのデータにアクセスする方法がわからないことです。私が何かをするなら;

user.profiles[1].followers[1]

必要なプロファイルが表示されます。私も次のようなものが欲しいです。

user.profiles[1].followers[1].assigned_group

その関係が属するグループにアクセスできました。

私のデザインは間違っていますか、それともここで何かを見落としていますか?

4

2 に答える 2

0

答えに近づいています。has_many にブロックを渡しています

has_many accepted_relationships... do
  def assigned_group
    #code
  end
end

まだ完全には理解できていませんが、これが私が取るべき道だと思います。

于 2013-06-06T07:49:32.853 に答える
0

あなたのグループには多くのプロファイルがあり、グループには関係が必要ですか。グループ内に関係またはプロファイル、または関係を介してプロファイルを持つことができます

于 2013-05-22T08:53:31.150 に答える