我有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
所以我可以访问该关系所属的组。
是我的设计关闭了,还是我在这里忽略了某些东西?